解析云代码 - 承诺问题
Parse cloud code - issues with promises
这是我的代码
Parse.Cloud.define('filters', function(request, response){
var _ = require('underscore');
var customerGeoPoint = request.params.geolocation;
var rating = request.params.rating
var finalList = [];
// var arr = [];
var promises = [];
var query = new Parse.Query('ServiceProvider');
query.withinMiles("geoLocation", customerGeoPoint, 10);
query.find().then(function(spobjs){
return spobjs
}).then(function(newval){
var query2 = new Parse.Query('Customer');
for(i in newval){
query2.withinMiles('geoLocation', newval[i].get('geoLocation'), newval[i].get('serviceRadius'));
var sp = query2.first()
if(sp != null)
{
finalList.push(newval[i]);
}
}
return finalList ;
}).then(function(resval){
var arr = [];
var arr = _.sortBy(resval,'averageRating'); ** This Line doesn't work **
console.log(arr[0]);
return arr ;
}).then(function(checkval){
response.success(checkval);
},function(error){
// console.log(error);
response.error(error);
});
});
在上面的代码中,"This Line doesn't work" 行什么都不做。我需要 underscore.js
但它仍然没有对数组进行排序。 finalList
值返回到 then
promise
之后,但它不会对它进行排序并且 returns 与 finalList
相同的值。有人可以告诉我这段代码有什么问题吗?
当使用 underscorejs 对解析对象进行排序时,迭代对象必须 return 使用 get()
...
的属性值
var arr = _.sortBy(resval, function(o) { return o.get('averageRating'); });
这是我的代码
Parse.Cloud.define('filters', function(request, response){
var _ = require('underscore');
var customerGeoPoint = request.params.geolocation;
var rating = request.params.rating
var finalList = [];
// var arr = [];
var promises = [];
var query = new Parse.Query('ServiceProvider');
query.withinMiles("geoLocation", customerGeoPoint, 10);
query.find().then(function(spobjs){
return spobjs
}).then(function(newval){
var query2 = new Parse.Query('Customer');
for(i in newval){
query2.withinMiles('geoLocation', newval[i].get('geoLocation'), newval[i].get('serviceRadius'));
var sp = query2.first()
if(sp != null)
{
finalList.push(newval[i]);
}
}
return finalList ;
}).then(function(resval){
var arr = [];
var arr = _.sortBy(resval,'averageRating'); ** This Line doesn't work **
console.log(arr[0]);
return arr ;
}).then(function(checkval){
response.success(checkval);
},function(error){
// console.log(error);
response.error(error);
});
});
在上面的代码中,"This Line doesn't work" 行什么都不做。我需要 underscore.js
但它仍然没有对数组进行排序。 finalList
值返回到 then
promise
之后,但它不会对它进行排序并且 returns 与 finalList
相同的值。有人可以告诉我这段代码有什么问题吗?
当使用 underscorejs 对解析对象进行排序时,迭代对象必须 return 使用 get()
...
var arr = _.sortBy(resval, function(o) { return o.get('averageRating'); });