分析数据库作业没有从http请求中获取所有结果
Parse database job is not getting all results from http request
我在应用程序的解析云代码中设置了以下代码作为作业。
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));
});
});
相关文章:
- D3 Json请求获取XmlHttpRequest错误:访问控制允许原点不允许原点为null
- 找不到Angular+Ionic Post请求获取404
- 如何从 ajax 请求获取数据响应
- 在Sencha touch 2.0中使用Ajax请求获取cookie
- 在 Python 中使用请求获取页面但不使用源代码,为什么?如何获取源代码
- 从javascript Http请求获取Java List
- Django View 无法使用请求获取文件.来自 ajax 表单的文件提交
- GiantBomb API 请求获取 HTML 而不是 JSON,Nodejs 请求模块
- 使用 ajax 请求获取上一个和下一个对象
- 从AJAX请求获取javascript变量的数字时遇到问题
- JQuery请求获取超过10个RSS提要
- XEP-0133:管理员请求获取所有在线用户列表
- 通过javascript通过ajax请求获取页面的完整html源代码
- NodeJS/Angular编辑请求获取400错误
- 从NodeJS请求获取主机
- 从http请求获取包信息
- 使用请求获取API结果,并集成到Hubot响应中
- 通过jQuery ajax请求获取数据
- Fcbkcomplete用ajax请求获取数据
- Node.js请求获取一个VAST标签