在云函数中的Parse.Cloud.httpRequest中生成并保存ParseObjects列表

use*_*182 5 javascript database android parse-platform

所以,我正在定义一个云函数,它应该调用foursquare api并从返回的JSON生成一个餐馆列表(每个餐馆都是一个ParseObject).我成功地做到了这一点,但是在尝试将这些对象保存到我的数据库并通过调用response.success()将它们发送回我的手机时遇到了问题.下面的大代码块将列表保存到我的数据库中,但是如果我尝试的话

Parse.Object.saveAll(restaurants)
response.success(restaurants)
Run Code Online (Sandbox Code Playgroud)

我在保存所有餐馆之前结束了这个功能.我尝试使用这一行代替

Parse.Object.saveAll(restaurants).then(response.success(restaurants))
Run Code Online (Sandbox Code Playgroud)

,但只有一半的餐馆在我收到错误之前得到保存"失败:未捕获尝试使用指向未保存的新对象的对象保存对象".如果我在不尝试保存列表的情况下调用response.success(餐馆),我也会收到此错误.我读到这是解析中的一个错误,阻止某人打印或传递未保存的ParseObjects.有任何想法吗?我也尝试在http请求上使用.then,但是我遇到了同样的问题或者出现了新的错误:"com.parse.ParseException:i/o failure:java.net.SocketTimeoutException:read timed out."

Parse.Cloud.define("callFourSquare", function(request, response) {
//The Parse GeoPoint with current location for search
    var geo = request.params.location;
    var geoJson = geo.toJSON();
    var url = "https://api.foursquare.com/v2/venues/explore?ll=" + geoJson.latitude + ","
        + geoJson.longitude +         "&section=food&sortByDistance=1&limit=50&venuePhotos=1&categoryId=4d4b7105d754a06374d81259&client_id=    C043AJBWKIPBAXOHLPA0T40SG5L0GGMQRWQCCIKTRRVLFPTH" 
        + "&client_secret=Y1GZZRHXEW1I3SQL3LTHQFNIZRDCTRG12FVIQI5QGUX0VIZP&v=20140715";
        console.log(url);
    //Call to FourSquare api, which returns list of restaurants and their details
    Parse.Cloud.httpRequest({
        method: "GET",
        url: url,
        success: function (httpResponse) {
            var restaurants = [];
            var json = httpResponse.data;
            var venues = json.response.groups[0].items;
            console.log(venues.length)
            for(i = 0; i < venues.length; i++) {
                venue = venues[i].venue;

                var RestaurantObject =  Parse.Object.extend("Restaurant");
                var rest = new RestaurantObject();
                try {
                    rest.set("geoLocation", 
                    new Parse.GeoPoint({latitude: venue.location.lat, 
                        longitude: venue.location.lng}));

                } catch(err) {}
                try {
                    rest.set("address", venue.location.address + " " +     venue.location.formattedAddress[1]);
                } catch(err) {}
                try {
                    rest.set("phoneNumber", venue.contact.formattedPhone);
                } catch(err) {}
                try {
                    rest.set("website", venue.url);
                } catch(err) {}
                rest.set("name", venue.name);
                rest.set("lowerName", venue.name.toLowerCase());
                try {
                    rest.set("priceLevel", venue.price.tier);
                } catch(err) {}
                try {
                    rest.set("rating", venue.rating/2);
                } catch(err) {}
                try {
                    rest.set("storeId", venue.id);
                } catch(err) {}
                try {
                    rest.set("icon", venue.photos.groups[0].items[0].prefix + "original"
                        + venue.photos.groups[0].items[0].suffix)
                } catch(err) {}

                restaurants.push(rest);

            }
            Parse.Object.saveAll(restaurants);
        },
        error: function (httpResponse) {
            response.error("Request failed with response code:" + httpResponse.status + "     Message: "
                + httpResponse.text);
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

Tro*_*dal 0

我相信您的问题是,当您的 httpRequest() 完成时,您没有从 Parse.Object.saveAll(restaurants) 返回 Promise。尝试返回 saveAll() 承诺并查看它是否完成。