Parse.com更新用户Cloud Code给出错误206:无法修改用户 [英] Parse.com update user Cloud Code giving error 206: cannot modify user
问题描述
我正在尝试在Parse Server上编写一个简单的Cloud Code函数,以更新用户的参数.我的功能如下:
I am trying to write a simple Cloud Code function on Parse Server that updates a user's paramater. My function is as follows:
Parse.Cloud.define("updateUser", function(request, response) {
var query = new Parse.Query("User");
query.equalTo("username", request.params.username);
var type = request.params.type;
query.first({
success: function (user) {
user.set("userType", type);
user.save(null, {
success: function (object) {
response.success("SUCCESS");
}, error: function (object, error) {
response.error(error);
}
});
}, error: function (error) {
response.error(error);
}
});
});
这就是我从Express Node.js应用程序调用函数服务器端的方式:
And this is how I call the function server-side from an Express Node.js app:
Parse.Cloud.run("updateUser", { useMasterKey: true, username: req.body.username, type: req.body.type }, {
success: function(final) {
res.send({ res: "SUCCESS" });
}, error: function(error) {
res.send({ res: error });
}
});
第一个查询返回一个用户,但随后的保存将引发"206:无法修改用户"错误.
The first query returns a user, but the subsequent save throws a "206: cannot modify user" error.
此功能在我们以前的平台上效果很好,但现在在Parse Server上引起了问题.
This function worked great on our previous platform, but now is causing issues on Parse Server.
非常感谢您的帮助.
推荐答案
应该将useMasterKey发送到您的云代码方法的user.save(),并在您的代码中从nodeJS应用发送了useMasterKey.
the useMasterKey should be send to the user.save() of your cloud code method and in your code you sent the useMasterKey from your nodeJS app.
因此,最后,您的代码应如下所示:
so at the end your code should look like the following:
Parse.Cloud.define("updateUser", function(request, response) {
var query = new Parse.Query("User");
query.equalTo("username", request.params.username);
var type = request.params.type;
query.first({
success: function (user) {
user.set("userType", type);
user.save({useMasterKey : true}, {
success: function (object) {
response.success("SUCCESS");
}, error: function (object, error) {
response.error(error);
}
});
}, error: function (error) {
response.error(error);
}
});
});
我还将根据最佳实践将代码更改为使用Promises和arrow函数.
I would also change my code to use Promises and arrow functions according to the best practices.
希望有帮助.
这篇关于Parse.com更新用户Cloud Code给出错误206:无法修改用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!