Parse.Cloud.beforeSave(Parse.User, function(request, response) {
var query = new new Parse.Query(Parse.User);
query.count({
success: function(count) {
request.object.set("membersNumber", count+1);
response.success();
},
error: function(error) {
response.success();
}
});
});
在Parse后端,我想为每个注册的用户分配一个序号。例如,第一个成员将获得membersNumber = 1,第二个成员将获得membersNumber = 2等。
但上面的代码失败了。计数始终为零。我怀疑beforeSave函数没有主凭据来获取所有用户对象?
如何计算此beforeSave方法中的总用户数?
答案 0 :(得分:1)
找到答案。需要在count方法中传递useMasterKey:true。
Parse.Cloud.beforeSave(Parse.User, function(request, response) {
if(request.user == undefined){
//new sign up
var query = new Parse.Query(Parse.User);
query.count({
useMasterKey:true,
success: function(count) {
var membersNumber = count+1;
request.object.set("membersNumber", membersNumber);
response.success();
},
error: function(error) {
response.success();
}
});
}else{
response.success();
}
});