我正在尝试编写一个 Parse Cloud Code 函数,其中参数是一个对象列表,每个对象都包含一个 geoPoint 成员。对于列表中的每个项目,我将在 Parse 数据存储中搜索 1 英里半径内具有相同名称的现有项目。如果该项目不存在,则创建该项目并将其保存到数据存储中。
我的功能
/**
* Take a list of Place and check for existence in Parse.
* If it doesn't already exist, add it.
* @see https://www.parse.com/questions/access-distance-on-a-geoquery
* @param {JSON Place.PlaceSearch}
* @return none
*/
function addConvertedApiResult(placeData) {
for ( var i = 0; i < placeData.length; i++ ) {
// look near loc for existing name
var loc = new Parse.GeoPoint(placeData[i].lat, placeData[i].lon)
var query = new Parse.Query("Place");
query.withinMiles("location", loc, 1);
query.equalTo("name", placeData[i].name);
console.log("placeData[i].name:"+placeData[i].name);
query.find({
success: function(results) {
// results contains a list of places that are within 1 mile
// and have the same name
console.log("results.length = "+results.length);
if(results.length < 1) {
// add the place
var Place = Parse.Object.extend("Place");
var place = new Place();
place.set("en", placeData[i].name);
place.set("location", loc);
place.set("moreInfo", placeData[i].moreInfo);
place.save();
}
},
error: function(error) {
// There was an error.
console.log("error = "+error.message);
}
});
console.log("leaving addConvertedApiResult");
}
}
我对调用 addConvertedApiResult 的云函数的请求
(必须从 Windows 命令行中转义 '"')
curl -X POST
-H "X-Parse-Application-Id: <key>"
-H "X-Parse-REST-API-Key: <key>" -H "Content-Type: application/json"
-d "{\"name\":\"Storm+Mountain\",\"lat\":44.0760,\"lon\":-103.2280,\"limit\":5,\"radius\":25}" https://api.parse.com/1/functions/getPlace
{"result":[{"lat":43.95483,"lon":-103.36869,"moreInfo":"<apiUrl>/item.php?c=1\u0026i=3439","name":"Storm Mountain"}]}
产生的解析信息日志
I2015-03-03T05:56:26.905Z] v99: Ran cloud function getPlace with:
Input: {"lat":44.0760,"limit":5,"lon":-103.2280,"name":"Storm+Mountain","radius":25}
Result: [{"name":"Storm Mountain","lat":43.95483,"lon":-103.36869,"moreInfo":"<moreInfoUrl>"}]
I2015-03-03T05:56:27.434Z] placeData[i].name:Storm Mountain
I2015-03-03T05:56:27.435Z] leaving addConvertedApiResult
应该返回数据存储中的 4 个现有点,但它们都不具有相同的名称。该函数似乎没有执行 query.find 方法。我没有看到来自 success: 或 error: 函数的日志消息。如果我理解正确,这些函数应该允许我在查询结果上执行代码。
如果 console.log 似乎不起作用,我如何确认查询没有结果?
我一直在网上尝试这种语法的不同变体。我的语法正确吗?
谢谢。
更新
我一直在再次解决这个问题,并且很高兴了解Promises。不幸的是,我的情况并没有改变。我现在从我的addConvertedApiResult函数中调用这些函数。我已经使用 Chrome 开发者工具使用本地 javascript 文件中编写的驱动程序对它们进行了测试/调试,一切运行良好。但是,当我将此代码部署到 Parse Cloud 时,调用 .find() 后,代码执行似乎消失了。
我知道代码执行在 CC 上是异步的,但我的印象是使用 .then() 函数应该可以克服这个限制。
我希望有人可以向我解释我错过了什么。
谢谢。
/**
* Take a Place and check for existence in Parse.
* If it doesn't already exist, add it.
* @see https://www.parse.com/questions/access-distance-on-a-geoquery
* @see https://parse.com/docs/js_guide#promises-chaining
* @param {JSON Place.PlaceSearch}
* @return none
*/
function addNewPlace(place) {
// look near loc for already existing place with same name
var loc = new Parse.GeoPoint(place.lat, place.lon)
var query = new Parse.Query('Place');
query.withinMiles('location', loc, 1);
query.equalTo('en', place.name);
query.find().then( function(results) {
if(results.length < 1) {
console.log(place.name+" does not exist")
var Place = Parse.Object.extend("Place");
var newPlace = new Place();
var loc = new Parse.GeoPoint(place.lat, place.lon)
newPlace.set('en', place.name);
newPlace.set('location', loc);
newPlace.set('moreinfo', place.moreinfo);
return newPlace.save();
}
});
}
/**
* Take a list of Place and check for existence in Parse.
* If it doesn't already exist, add it.
* @see https://www.parse.com/questions/access-distance-on-a-geoquery
* @see https://parse.com/docs/js_guide#promises-chaining
* @param {JSON Place.PlaceSearch}
* @return none
*/
function addConvertedApiResult(placeData) {
var _ = require('underscore');
console.log("placeData.legth:"+placeData.length);
_.each(placeData, function(place) {
addNewPlace(place);
});
}