简体   繁体   English

解析云代码-承诺问题

[英]Parse cloud code - issues with promises

Here is my 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);
});

});

In the above code the line which reads "This Line doesn't work" does nothing. 在上面的代码中,显示“此行不起作用”的行无效。 I have required underscore.js but still it doesn't sort the array. 我需要underscore.js但仍然无法对数组进行排序。 finalList value gets returned to the then promise after but it doesn't sort it and returns the same value as finalList . finalList值将返回至then promisethen不进行排序,并返回与finalList相同的值。 Can someone tell me what's the issue with this code? 有人可以告诉我这段代码有什么问题吗?

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM