Returning an object from a javascript function with a database query inside

I'm trying to return markers as an object, but when I run the function, it just returns [], but printing it inside, I can see the data of the object, can anyone explain how to return the batch2 object, please?

google.maps.event.addListener(mgr, 'loaded', function(){ mgr.addMarkers(getMarkers(),6); //add all the markers! documentation for viewports with totals for city count, look at viewport mgr.addMarkers(getMarkers2(),14); //get markers for zoomed out place, add click function to zoom in //mgr.addMarkers(getMarkers(1000), 8); console.log("added"); mgr.refresh(); }); function getMarkers2() { var batch2 = []; var clusters = new Parse.Query("cityfreqcoords"); var clusterresults = new Parse.Object("cityfreqcoords"); clusters.find({ success: function (results) { for (i = 1; i < results.length; i++) { var city = (results[i]["attributes"]["city"]); var count = (results[i]["attributes"]["count"]); var lat = (results[i]["attributes"]["lat"]); var lng = (results[i]["attributes"]["lng"]); var markerLatlong = new google.maps.LatLng(lat, lng); //icon = //adding the marker var marker2 = new google.maps.Marker({ position: markerLatlong, title: city, clickable: true, animation: google.maps.Animation.DROP //icon:icon }); //adding the click event and info window google.maps.event.addListener(marker2, 'click', function () { map.setZoom(6); map.setCenter(marker2.getPosition()); }); batch2.push(marker2); } } }) return batch2; } 
+6
source share
3 answers

The transition simply ended, making the token manager global and passing mgr to the requested file may not be the most efficient way to do this

 function getMarkers2(mgr) { Parse.initialize("X", "Y"); var batch2 = []; var clusters = new Parse.Query("cityfrequency2"); var clusterresults = new Parse.Object("cityfrequency2"); clusters.find({ success: function (results) { for (i = 0; i < (results.length); i++) { var city = (results[i]["attributes"]["city"]); var lat = (results[i]["attributes"]["lat"]); var lng = (results[i]["attributes"]["lng"]); var markerLatlong = new google.maps.LatLng(lat, lng); var image = { url: 'warning.png', size: new google.maps.Size(50, 46), // The origin origin: new google.maps.Point(0, 0), // The anchor anchor: new google.maps.Point(25, 0) }; //adding the marker var marker2 = new google.maps.Marker({ position: markerLatlong, title: city, clickable: true, animation: google.maps.Animation.DROP, icon:image }); //adding the click event and info window google.maps.event.addListener(marker2, 'click', function () { map.setZoom(6); map.setCenter(); }); batch2.push(marker2); mgr.addMarkers(batch2,0,6); mgr.refresh(); } } }) } function setupMarkers() { var mgrOptions = { borderPadding: 50, maxZoom: 15, trackMarkers: true }; mgr = new MarkerManager(map,mgrOptions); google.maps.event.addListener(mgr, 'loaded', function(){ getMarkers2(mgr); getMarkers(mgr); console.log("added"); }); } 
0
source

It seems that clusters.find is asynchronous. You return batch2 before reaching cluster.find . There are several patterns for working with asynchronous code in JavaScript - one of them is using a callback. You will need to rewrite your code like this:

 function getMarkers2(callback) { var batch2 = []; var clusters = new Parse.Query("cityfreqcoords"); var clusterresults = new Parse.Object("cityfreqcoords"); clusters.find({ success: function (results) { for (i = 1; i < results.length; i++) { var city = (results[i]["attributes"]["city"]); var count = (results[i]["attributes"]["count"]); var lat = (results[i]["attributes"]["lat"]); var lng = (results[i]["attributes"]["lng"]); var markerLatlong = new google.maps.LatLng(lat, lng); //icon = //adding the marker var marker2 = new google.maps.Marker({ position: markerLatlong, title: city, clickable: true, animation: google.maps.Animation.DROP //icon:icon }); //adding the click event and info window google.maps.event.addListener(marker2, 'click', function () { map.setZoom(6); map.setCenter(marker2.getPosition()); }); batch2.push(marker2); } } callback(batch2); }) } 

Then name it like this:

 getMarkers2(function(markers) { mgr.addMarkers(markers, 14); }); 

If you're interested, see how promises works, as you might prefer this approach using callbacks.

+4
source

With javascript callbacks, you usually don't return data. You pass another function reference to the handler as a callback.

EG:

 function getMarkers2(f) { // do stuff //when done f(batch2) } 
0
source

Source: https://habr.com/ru/post/989632/


All Articles