简体   繁体   中英

How do I import data from javascript into a mongoDB database?

I am attempting to use a geolocation script that collects the location of the user. I'll also eventually add a geocoder but that can wait. This is the script:

<script>
        var x = document.getElementById("demo");
        function getLocation() {
            if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(showPosition);
            } else {
                x.innerHTML = "Geolocation is not supported by this browser.";
            }
        }

        function showPosition(position) {
            x.innerHTML = "Latitude: " + position.coords.latitude +
            "<br>Longitude: " + position.coords.longitude;
        }

    </script>

My goal is to take the data that was collected about the user, and store it into a database using mongoDB where I can see it. I am new to javascript but I know how to use mongoDB fairly well. Can anybody help me?

you cannot do it without backend language like node js or php or something else. You need to create api in backend language and then you can use frontend js to hit that api and get and store results.

As pointed out in answer above by @DHRUV GUPTA you need a backend and that is for both security as connecting to the db shouldnt be from the browser side and also to be able to use packages like mongoose . It is pretty similar in format to the mongoshell and shouldn't take too long to get used

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM