解析云代码 - 承诺问题

时间:2015-12-07 04:39:47

标签: javascript parse-platform promise underscore.js cloud-code

这是我的代码

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

});

在上面的代码中,读取"此行不起作用的行"什么也没做。我需要underscore.js,但它仍然没有对数组进行排序。 finalList值会在then promise之后返回finalList {{1}},但它不会对其进行排序并返回与{{1}}相同的值。有人可以告诉我这段代码的问题是什么吗?

1 个答案:

答案 0 :(得分:2)

使用underscorejs对解析对象进行排序时,iteratee必须使用get()返回属性的值...

var arr = _.sortBy(resval, function(o) { return o.get('averageRating'); });