Tuesday 14 March 2017

NodeJS Callbacks working

I am creating a NodeJs application. I need to have the following functionality in it.

1. From the user data the location is requested using GoogleMap API.
2. From the JSON object received latitudes and longitudes are found out.
3. The longitude and latitudes are saved to the corresponding entries in MongoDB

I have got the following code

var options = {
               host: 'maps.googleapis.com',
               path: '/maps/api/geocode/json?&address=' + donor[j].pincode + '&key=AIzaSyD4M1TkzIl0nyKObyXtrCOgHJpN_BDPb6A'
               }
https.request(options,callback).end();
callback = function (response) {
                                var str = '';
                                response.on('data', function (chunk) {
                                    str += chunk;
                                });
                                response.on('end', function () {
                                    var loc = JSON.parse(str);
                                    console.log(loc.results[0].geometry.location.lat); 
                                    Donor.collection.updateOne({`email:email`},{$set: {latitude:loc.results[0].geometry.location.lat,longitude:loc.results[0].geometry.location.lat}},false,true);
                                });
                            } 

In the line Donor.collection.updateOne({email:email},{$set: {latitude:loc.results[0].geometry.location.lat,longitude:loc.results[0].geometry.location.lat}},false,true); I want to pass the email field from the calling function to check for that email and update the latitide and longitude.

How will I do that?



via Karthik S Kumar

No comments:

Post a Comment