解析数据库作业未从http请求获取所有结果

时间:2016-01-05 21:20:31

标签: javascript json web-services parse-platform

我将以下代码设置为我的应用程序的Parse Cloud Code中的作业。

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");

    for (var i = 0; i < 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 to create object, with error code: " + error.message);
            }
        });
    }
}, function (httpResponse) {
    // Error
    response.error('request failed with response code ' + httpResponse)
});});

如您所见,此HTTP请求应返回总共14个位置。不幸的是,它只返回9个位置,而且似乎返回的9个可以改变。我假设我的功能放在一起的方式有问题。任何人都可以帮我解决这个问题。我想根据HTTP请求的半径返回尽可能多的地方。

谢谢

1 个答案:

答案 0 :(得分:0)

http请求正确完成,承诺在请求完成时完成。但是你的then()块尝试在循环中创建多个对象,而不是等待它们全部完成,并且无法调用response.success。像这样修复......

div