在用户注册时如何将其添加到现有角色?我下面的代码不起作用,日志中没有任何错误或打印(不确定我在做什么):

Parse.User.signUp(req.body.email, req.body.password, {
    "name": req.body.name,
    "email": req.body.email,
}, {
    success: function(user) {
        var query = new Parse.Query(Parse.Role);
        query.equalTo("name", 'Basic User');

        query.find({
            success: function(roles) {
                alert("HELLO" + roles);

                if (roles.length < 1) {
                    alert("HELLO");

                    console.log("Error Signing Up: No roles for " + signUpRoleGroup + " were found");
                } else {
                    alert("HERE");

                    roles[0].getUsers().add(user);
                    roles[0].save();
                }
            },
            error: function(error) {
                alert("Could not add users to the account " + accountName + " error: " + error.message);
            }
        });
    },
    error: function(user, error) {
        alert("Error signing up user");
    }
});

最佳答案

我认为发生了什么事,就是呼叫者没有给此功能一个完成保存角色的机会。在调用response.success()或error()之前,云函数应该等待直到角色被保存。

这是对相同代码的承诺返回重写。我认为,对序列进行的异步任务更轻松,也更清楚。这使您可以更牢固地控制响应。

Parse.Cloud.define("signupAsBasicUser", function(request, response) {
    signupAsBasicUser(request.params.name, request.params.password, request.params.email).then(function(user) {
        response.success(user);
    }, function(error) {
        response.error(error);
    });
});


// return a promise fulfilled with a signed-up user who is added to the 'Basic User" role
//
function signupAsBasicUser(name, password, email) {
    Parse.Cloud.useMasterKey();
    var user = new Parse.User();
    user.set("username", name);
    user.set("password", password);
    user.set("email", email);
    return user.signUp().then(function() {
        var query = new Parse.Query(Parse.Role);
        query.equalTo("name", 'Basic User');
        return query.find();
    }).then(function(roles) {
        if (roles.length < 1) return Parse.Promise.error("no such role");
        roles[0].getUsers().add(user);
        return roles[0].save();
    }).then(function() {
        return user;
    });
}

关于javascript - Parse.com:将用户注册为 Angular 色,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28702524/

10-10 00:08