9

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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Ouch, an "adaptive when()" falls somewhere between magnificent and confusing. Will take some getting used to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.