简体   繁体   English

Parse.com CloudCode 将用户添加到现有角色不起作用

[英]Parse.com CloudCode add user to existing role not working

Here is the Parse javascript cloud code I am trying to use.这是我尝试使用的 Parse javascript 云代码。 As a new _User is created I want to add them to my 'Client' Role.创建新的 _User 后,我想将它们添加到我的“客户”角色中。

Parse.Cloud.afterSave(Parse.User, function(request) {
    Parse.Cloud.useMasterKey();

    query = new Parse.Query(Parse.Role);
    query.equalTo("name", "Client");
    query.first ({
        success: function(role) {
            role.getUsers().add(request.user);
            role.save();
        },
        error: function(error) {
            throw "Got an error " + error.code + " : " + error.message;
        }
    });
});

This is taking code directly from Parse.com's Role example.这是直接从 Parse.com 的 Role 示例中获取代码。 The code runs happily when a new _User is saved, returning Result: Success , but when I check the "users" tied to that Role in the Data Browser, nothing has happened.当保存新的 _User 时,代码运行愉快,返回Result: Success ,但是当我在数据浏览器中检查与该角色相关的“用户”时,什么也没发生。

I have also tried substituting role.getUsers().add(request.user);我也试过替换role.getUsers().add(request.user); for role.relation("users").add(request.user);对于role.relation("users").add(request.user); as per an example on Parse.com's old forum, but no difference.根据 Parse.com 旧论坛上的示例,但没有区别。 This seems like it should be really straight forward, so I'm not sure what I'm doing wrong.这似乎应该很简单,所以我不确定我做错了什么。

(I have manually used the REST API, using curl, to add _Users to the Client Role, and this does work, so I know it should work.) (我已经手动使用 REST API,使用 curl,将 _Users 添加到客户端角色,这确实有效,所以我知道它应该有效。)

Turns out you need to use request.object instead of request.user .原来你需要使用request.object而不是request.user Now it works!现在它起作用了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM