javascript - Parse database job is not getting all results from http request -


i have following code set job in parse cloud code application.

parse.cloud.job("requestlocations", function (request, response) {parse.cloud.httprequest({     url: 'https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=29.7030428,-98.1364808&radius=900&types=restaurant&key=aizasyctg0x68q6lrcao6-a37zkxge81jdekpvo' }).then(function (httpresponse) {     // success     response.success("success");     var parseddata = json.parse(httpresponse.text);     var location = parse.object.extend("locations");      (var = 0; < parseddata.results.length; i++) {         var restaurant = new location();         var placeid = parseddata.results[i].place_id;         var name = parseddata.results[i].name;         var vicinity = parseddata.results[i].vicinity;         var point = new parse.geopoint({             latitude: parseddata.results[i].geometry.location.lat,             longitude: parseddata.results[i].geometry.location.lng         });          restaurant.set("placeid", placeid);         restaurant.set("name", name);         restaurant.set("vicinity", vicinity);         restaurant.set("location", point);         restaurant.save(null, {             success: function (location) {                 console.log("object id: " + location.id);             },             error: function (location, error) {                 console.log("failed create object, error code: " + error.message);             }         });     } }, function (httpresponse) {     // error     response.error('request failed response code ' + httpresponse) });}); 

as can see, http request should return total of 14 places. unfortunately, return 9 places , seem 9 return can change. assuming there problem way function put together. can me remedy issue. return many places want based on radius of http request.

thank you

the http request done right, promise that's fulfilled when request complete. then() block tries create several objects in loop, not waiting them finish, , failing call response.success. fix this...

// break understandable chunks, too, so, here's function // build locations object http data  function locationfromresult(result) {     var location = parse.object.extend("locations");     var restaurant = new location();     var placeid = result.place_id;     var name = result.name;     var vicinity = result.vicinity;     var point = new parse.geopoint({         latitude: result.geometry.location.lat,         longitude: result.geometry.location.lng     });     restaurant.set("placeid", placeid);     restaurant.set("name", name);     restaurant.set("vicinity", vicinity);     restaurant.set("location", point);     return restaurant; }  parse.cloud.job("requestlocations", function (request, response) {     var url = 'https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=29.7030428,-98.1364808&radius=900&types=restaurant&key=aizasyctg0x68q6lrcao6-a37zkxge81jdekpvo';     parse.cloud.httprequest({url: url}).then(function (httpresponse) {         var parseddata = json.parse(httpresponse.text);         var locations = parseddata.results.map(function(result) {             return locationfromresult(result);         });         // important, saveall of new objects before returning         // can accomplished saving objects individually , using parse.promise.when()         return parse.object.saveall(locations);     }).then(function(result) {         response.success(json.stringify(result));     }, function(error) {         response.error(json.stringify(error));     }); }); 

Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -