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

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

问题描述

所以,我定义的云功能的应该打电话到Foursquare的API和生成的餐馆列表(每家餐厅是的parseObject)从返回的JSON。我成功地做到这一点,但我遇到问题想保存这些对象到我的数据库,并将其通过调用response.success发回我的电话时,()。大code座下面的列表保存到我的数据库,但如果我尝试

  Parse.Object.saveAll(餐馆)
response.success(餐馆)
 

我结束之前,所有的餐馆都保存功能。我尝试使用这一行,而不是

  Parse.Object.saveAll(餐厅)。然后(response.success(餐厅))
 

,但只有一半得到保存之前,我得到错误的餐馆与失败:未捕获试图挽救一个对象指向一个新的,未保存的对象我也得到这个错误,如果我叫response.success(餐厅)不试图保存列表。我看,这是pventing有人从打印或通过保存的ParseObjects在解析$ P $的错误。有任何想法吗?我也尝试过使用。后来在HTTP请求,但我得到了同样的问题或新的错误:com.parse.ParseException:I / O故障:java.net.SocketTimeoutException:读超时

  Parse.Cloud.define(callFourSquare功能(请求,响应){
//解析的GeoPoint当前位置搜索
    无功缘= request.params.location;
    变种geoJson = geo.toJSON();
    变种的url =htt​​ps://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&安培;ν= 20140715;
        执行console.log(URL);
    //调用到四角的API,它返回的餐馆和他们的详细资料列表
    Parse.Cloud.htt prequest({
        方法:GET,
        网址:网址,
        成功:函数(HTT presponse){
            VAR餐馆= [];
            VAR JSON = HTT presponse.data;
            变种场地= json.response.groups [0] .items;
            执行console.log(venues.length)
            对于(i = 0; I< venues.length;我++){
                会场=场馆[I] .venue;

                VAR RestaurantObject = Parse.Object.extend(餐厅);
                VAR休息=新RestaurantObject();
                尝试 {
                    rest.set(地理位置,
                    新Parse.GeoPoint({纬度:venue.location.lat,
                        经度:venue.location.lng}));

                }赶上(错误){}
                尝试 {
                    rest.set(地址,venue.location.address ++ venue.location.formattedAddress [1]);
                }赶上(错误){}
                尝试 {
                    rest.set(phoneNumber的,venue.contact.formattedPhone);
                }赶上(错误){}
                尝试 {
                    rest.set(简称网站,venue.url);
                }赶上(错误){}
                rest.set(名,venue.name);
                rest.set(lowerName,venue.name.toLowerCase());
                尝试 {
                    rest.set(priceLevel,venue.price.tier);
                }赶上(错误){}
                尝试 {
                    rest.set(等级,venue.rating / 2);
                }赶上(错误){}
                尝试 {
                    rest.set(STOREID,venue.id);
                }赶上(错误){}
                尝试 {
                    rest.set(图标,venue.photos.groups [0] .items [0]。preFIX +原生态
                        + venue.photos.groups [0] .items [0] .suffix)
                }赶上(错误){}

                restaurants.push(休息);

            }
            Parse.Object.saveAll(餐厅);
        },
        错误:函数(HTT presponse){
            response.error(请求失败,响应code:+ HTT presponse.status +留言:
                + HTT presponse.text);
        }
    });
});
 

解决方案

我相信你的问题是,你没有返回无极从Parse.Object.saveAll(餐馆),当你的HTT prequest()完成。尝试返回该白水回收()的承诺,看看它是否完成。

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

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

解决方案

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.htt prequest ParseObjects名单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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