在云函数中的 Parse.Cloud.httpRequest 中生成并保存 ParseObjects 列表 [英] Generating and saving a list of ParseObjects within a Parse.Cloud.httpRequest in a cloud function

查看:25
本文介绍了在云函数中的 Parse.Cloud.httpRequest 中生成并保存 ParseObjects 列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So, I'm defining a cloud function that's supposed to make a call to the foursquare api and generate a list of restaurants (each restaurant is a ParseObject) from the returned JSON. I successfully do this, but I run into problems when trying to save these objects to my database and send them back to my phone by calling response.success(). The large code block below saves the list to my database, but if I try

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

我在保存所有餐厅之前结束该功能.我尝试改用这条线

I end the function before all of the restaurants are saved. I tried using this line instead

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

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

, but only half of the restaurants get saved before I get the error "Failed with: Uncaught Tried to save an object with a pointer to a new, unsaved object." I also get this error if I call response.success(restaurants) without attempting to save the list. I read that this is a bug in parse preventing someone from printing or passing unsaved ParseObjects. Any ideas? I also tried using .then on the http request, but I get the same issues or a new error: "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);
        }
    });
});

推荐答案

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

I believe your issue is that you aren't returning the Promise from Parse.Object.saveAll(restaurants) when your httpRequest() is complete. Try returning that saveAll() promise and see if it completes.

这篇关于在云函数中的 Parse.Cloud.httpRequest 中生成并保存 ParseObjects 列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆