分析数据库作业没有从http请求中获取所有结果

Parse database job is not getting all results from http request

本文关键字:请求 获取 结果 http 数据库 作业      更新时间:2023-09-26

我在应用程序的解析云代码中设置了以下代码作为作业。

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请求的半径返回任意数量的位置。

谢谢

http请求完成得很好,在请求完成时会兑现承诺。但是你的then()块试图在一个循环中创建几个对象,而不是等待它们全部完成,并且无法调用response.success。。。

// break it into understandable chunks, too, so, here's a function
// to build a Locations object from the 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);
        });
        // this is important, saveAll of the new objects before returning
        // this can also be accomplished by saving the objects individually and using Parse.Promise.when()
        return Parse.Object.saveAll(locations);
    }).then(function(result) {
        response.success(JSON.stringify(result));
    }, function(error) {
        response.error(JSON.stringify(error));
    });
});