我想使用云代码功能在我的Parse.com应用程序中创建Role对象。 我试过的代码是:
Parse.Cloud.define("addRole",function(request,response) {
var publicReadACL = new Parse.ACL();
publicReadACL.setPublicReadAccess(true);
var user=Parse.User.current();
var baserolename=request.params.organisation;
var roleType=request.params.roleType;
var roleName=baserolename+"_"+roleType;
Parse.Cloud.useMasterKey();
var role = new Parse.Role(roleName,publicReadACL);
var occupants=role.getUsers();
occupants.add(user);
role.save(role).then (function(role) {
response.success(role.toJSON());
},
function(error) {
response.error(error);
});
});
运行此功能不会创建角色并给出{"code":105}
的响应 - 没有进一步的错误消息。 105似乎意味着"无效的密钥名称",但我不确定哪个密钥无效或为什么。
答案 0 :(得分:1)
我有一个稍微修改过的版本。这是我刚刚测试过的Typescript版本:
Parse.Cloud.define('addRole', (req: Parse.Cloud.FunctionRequest, res: Parse.Cloud.FunctionResponse) => {
let publicReadACL = new Parse.ACL();
publicReadACL.setPublicReadAccess(true);
let user = req.user;
let baseRoleName: string = req.params.organisation;
let roleType: string = req.params.roleType;
let roleName: string = `${baseRoleName}_${roleType}`;
Parse.Cloud.useMasterKey();
let role = new Parse.Role(roleName, publicReadACL);
let occupants = role.getUsers();
occupants.add(user);
role.save()
.then((role: Parse.Role) => {
res.success(role.toJSON())
}, res.error);
});
主要差异:
以下是已编译的Javascript版本:
Parse.Cloud.define('addRole', function (req, res) {
var publicReadACL = new Parse.ACL();
publicReadACL.setPublicReadAccess(true);
var user = req.user;
var baseRoleName = req.params.organisation;
var roleType = req.params.roleType;
var roleName = baseRoleName + "_" + roleType;
Parse.Cloud.useMasterKey();
var role = new Parse.Role(roleName, publicReadACL);
var occupants = role.getUsers();
occupants.add(user);
role.save()
.then(function (role) {
res.success(role.toJSON());
}, res.error);
});