0
Parse.Cloud.define("bulkUpdateUserViewedTraces", function(request, response){
    Parse.Cloud.useMasterKey();

    var userQuery = new Parse.Query(Parse.User);
    userQuery.limit(200);

    var userCount = 0;

    userQuery
        .find(function (users) {
            var promises = _.map(users, function (user){
                userCount++;
                return populateViewedTraces(user);
            });
            return promises;
        })
        .then(function (promises) {
            return Parse.Promise.when(promises);
        })
        .then(function(){
            console.log("Processed " + userCount + " users");
            response.success("Processed " + userCount + " users");
        }, function(error){
            console.error(JSON.stringify(error));
            response.error(error);
        });
});

function populateViewedTraces (user) {
    var viewedTracesCount = 0;

    var viewHistoryQuery = new Parse.Query("ViewHistory");
    viewHistoryQuery.equalTo("user", user);

    return viewHistoryQuery
        .each(function (viewHistory) {
            console.log("Got here");
            viewedTracesCount++;
        })
        .then(function () {
            console.log("Got here 2");
            return user.save();
        });
}

Doesnt work. Logs "Processed x users" but never logs "got here". The promises should execute during the when() call, but never do.

This makes absolutely no sense as every post I've seen describing when is written like this. For some reason this just doesnt do anything.

RonnyKnoxville
  • 6,166
  • 10
  • 46
  • 75
  • In `populateViewedTraces()`, `currentUser` remains undefined, therefore `currentUser.save()` will throw. Fix that first, then worry about `bulkUpdateUserViewedTraces`. – Roamer-1888 Aug 25 '15 at 18:09
  • I'm not too sure that `return viewHistoryQuery.each(...)` is correct either. – Roamer-1888 Aug 25 '15 at 18:12
  • Yes you are right, `currentUser` wasnt being used anymore. I've updated the code now. What do you think is wrong with `viewHistoryQuery.each(...)`? – RonnyKnoxville Aug 26 '15 at 08:32
  • I think the answer to this question is sort of worthless. Parse cannot run jobs over 10 seconds anyway. – RonnyKnoxville Aug 26 '15 at 08:35
  • I've not used `.each(...)` but the documentation says it's for iterating over results. Do you not need a `.find()` in there somewhere? – Roamer-1888 Aug 26 '15 at 11:08
  • 1
    It's OK, ignore me. According to the comment at the bottom of [this article](http://blog.parse.com/learn/engineering/launch-and-iterate-parse-data-migrations/) `.each()` "does a batch fetch". That's an important detail they forgot to mention in the documentation. Kinda fundamental really. – Roamer-1888 Aug 26 '15 at 11:18

0 Answers0