我有一个云代码,它在用户注册时创建两个帐户角色。下面是方法
Parse.Cloud.afterSave("account", function(request) {
var accountName = request.object.get("name");
//create admin role
var adminRoleACL = new Parse.ACL();
adminRoleACL.setPublicWriteAccess(true);
var adminRole = new Parse.Role(accountName + "_Administrator", adminRoleACL);
adminRole.save() ;
//create user role
var userRoleACL = new Parse.ACL();
userRoleACL.setPublicWriteAccess(true);
var userRole = new Parse.Role(accountName + "_User", userRoleACL);
userRole.save();
});
现在我想要实现的是添加刚刚注册到这两个角色的用户。但不幸的是,我看到在云代码中我无法获取当前用户。所以我所做的是在从客户端创建角色后将用户添加到角色中。下面是相同的代码。代码执行良好,我没有看到任何错误,但是我没有看到用户被添加到数据浏览器中的角色中。知道为什么会这样吗?我是不是错过了什么。我会非常感谢你的帮助。
user.signUp(null, {
success : function(user) {
var currentUser = Parse.User.current();
var accountName = account.get("name");
var query = new Parse.Query(Parse.Role);
query.contains("name", accountName);
query.find({
success : function(roles) {
if (!roles) {
alert("No roles for " + accountName + " were found");
} else {
for (var i = 0; i < roles.length; i++) {
//add the user for admin role
//TODO: this needs to be done only once for the account owner
if (roles[i].get("name").search(USER_TYPE.ADMIN) >= 0) {
roles[i].getUsers().add(currentUser);
}
//add the user for user role
if (roles[i].get("name").search(USER_TYPE.USER) >= 0) {
roles[i].getUsers().add(currentUser);
}
var saved = roles[i].save();
}
alert("User was added into roles");
}
},
error : function(error) {
alert("Could not add users to the account " + accountName + " error: " + error.message);
}
});
alert("User created successfully");
},
error : function(user, error) {
alert("Error: " + error.code + " " + error.message);
}
});