Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse Cloud code "first" query array returns different results

I have a simple query as follows :

var getGreaterQuestion = function (gid) {
    var query = new Parse.Query(Parse.Object.extend("Question"));
    query.equalTo("groupId", gid);
    return query.first();
}

I am preparing an array consisting of this function :

var groupIds = _.range(1, 17);
var groupIdAndRandomNumberPack = _.map(groupIds, function (gid) {
   return {groupId: gid, random: Math.random()};
});

var pack = _.map(groupIdAndRandomNumberPack, function (queryItem) {
   return getGreaterQuestion(queryItem.groupId, queryItem.random);
});

In pack array, there are 16 different "first" queries for Question class.

I am running this query using following code snippet :

return Parse.Promise.when(pack).then(function () {
        console.log("arguments : " + JSON.stringify(arguments));
...
...
);

arguments is the result of my query retrieving data from MongoDB.

If I run this query on parse backend, arguments json format is shown as below :

{
   "0":{QuestionObject},
   "1":{QuestionObject},
   ...
   "16":{QuestionObject}
}

If I run this query on my local parse instance with MongoDB defined on MongoLAB, it gives the following result :

{
   "0":[
      {QuestionObject},
      {QuestionObject},
      ....
      {QuestionObject}
   ]
}

What is the reason of this difference? Is there any configuration I need to apply on MongoDB or parse express application for getting the same result as parse backend gives.

like image 413
Fuat Coşkun Avatar asked Mar 09 '16 12:03

Fuat Coşkun


1 Answers

It is an issue with Parse.Promise.when function. Its implementation seems changed with this commit.

Back then when function was applying all results to your callback function regardless of how you supply inputs to when. These calls result to same output:

Parse.Promise.when([promise0, promise1, ...]).then(resultFunc)
Parse.Promise.when(promise0, promise1, ...).then(resultFunc)

var resultFunc = function() {
    console.log("arguments : " + JSON.stringify(arguments));
    //prints out like {"0": promiseResult0, "1", promiseResult1, ...}
}

Now, this behaviour is changed. It will return as one array if you supply input promises as array, it will apply as arguments if you do so. See this line in commit.

like image 168
erkangur Avatar answered Oct 05 '22 01:10

erkangur