Parse.com:用户表更新无效

时间:2014-09-23 15:28:51

标签: parse-platform

我已经定义了一个云功能来在User表中设置自定义列。我的查询返回了6个结果但是字段更新只发生了3行,我无法理解下面代码的错误,

Parse.Cloud.define("updateSaturdayAbsentWeeks", function(request, response) {
// User master key so that we can update all users
Parse.Cloud.useMasterKey();
// Tables to Query  
var query = new Parse.Query(Parse.User);
// We want only players who are absent on Saturdays
query.equalTo("isSaturdayAbsent", true);
query.find({
    success: function(results) {    
        console.error('updateSaturdayAbsentWeeks, Saturday absent players count ' + results.length);
        for (var i = 0; i < results.length; i++) { 
            var absentUser = results[i];
            var absentWeeks = absentUser.get('saturdayAbsentWeeks') - 1;
            absentUser.set("saturdayAbsentWeeks", absentWeeks);
            absentUser.save();
            console.error('updateSaturdayAbsentWeeks, absentWeeks for user name = ' + absentUser.get('username') + ', is = ' + absentWeeks)
        }

        // All done
        response.success("updateSaturdayAbsentWeeks finished successfully");
    },
    error: function() {
        console.error("runSaturdayExpense, lookUp Failed - Each player enjoyed the game on Saturday ");
        response.error("updateSaturdayAbsentWeeks failed");
    }
});
});

在查询之后我可以看到它返回了6条记录但是在for循环中它只更新了3条记录,并且没有对其他三条记录做任何事情并且没有返回错误。

任何能够理解为什么所有6条记录都没有得到更新的指针将不胜感激。

由于

1 个答案:

答案 0 :(得分:2)

这与save()函数是异步的这一事实有关。正确的方法是使用saveAll()

query.find({
    success: function(results) {
        var saveThese = []; 
        for (var i = 0; i < results.length; i++) { 
            var absentUser = results[i];
            var absentWeeks = absentUser.get('saturdayAbsentWeeks') - 1;
            absentUser.set("saturdayAbsentWeeks", absentWeeks);
            saveThese.push(absentUser);
        }
        Parse.Object.saveAll(saveThese, {
            success: function (list) {
                response.success("updateSaturdayAbsentWeeks finished successfully");
            }, 
            error: function (error) {
                response.error("did not save all the items");
            }
        });
    },
    error: function() {
        response.error("updateSaturdayAbsentWeeks failed");
    }
});