Search code examples
javascriptparse-platformpromiseunderscore.jsparse-cloud-code

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. finalList value gets returned to the then promise after but it doesn't sort it and returns the same value as finalList. Can someone tell me what's the issue with this code?


Solution

  • When sorting parse objects with underscorejs, the iteratee must return the value of the attribute using get()...

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