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

2023-12-19

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

Parse.Object.saveAll(restaurants)
response.success(restaurants)

我在所有餐厅都保存之前结束了该功能。我尝试使用这条线

Parse.Object.saveAll(restaurants).then(response.success(restaurants))

,但在我收到错误“失败原因:未捕获尝试使用指向新的未保存对象的指针保存对象”之前,只有一半的餐厅被保存。如果我调用response.success(restaurants)而不尝试保存列表,我也会收到此错误。我读到这是解析中的一个错误,阻止某人打印或传递未保存的 ParseObjects。有任何想法吗?我还尝试在http请求上使用.then,但我遇到了相同的问题或新错误:“com.parse.ParseException:i/o失败:java.net.SocketTimeoutException:读取超时。”

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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐