问题描述
我在使用 Parse Server JS SDK 编辑和保存用户时遇到问题.
I am having trouble using the Parse Server JS SDK to edit and save a user.
我正在登录,登录并检索用户就好了,我可以毫无例外地调用 user.set
并添加/编辑我想要的任何字段,但是当我尝试保存时,即使使用主密钥,我收到 Error 206: Can t modify user
.
I am signing in, logging in and retrieving the user just fine, I can call without exception user.set
and add/edit any field I want, but when I try to save, even when using the masterKey, I get Error 206: Can t modify user <id>
.
我也尝试过使用 save 直接设置字段,结果相同.
I also have tried to use save to direcly set the fields, same result.
有趣的是,在数据库中,用户的架构会随着新的字段和类型进行更新.
A interesting thing is that in the DB, the User's Schema get updated with the new fields and types.
这是我的更新功能:
function login(user, callback) {
let username = user.email,
password = user.password;
Parse.User.logIn(username, password).then(
(user) => {
if(!user) {
callback('No user found');
} else {
callback(null, user);
}
},
(error) => {
callback(error.message, null);
}
);
}
function update(user, callback) {
login(user, (error, user) => {
if(error) {
callback('Can t find user');
} else {
console.log('save');
console.log('Session token: ' + user.getSessionToken());
console.log('Master key: ' + Parse.masterKey);
user.set('user', 'set');
user.save({key: 'test'}, {useMasterKey: true}).then(
(test) => {
console.log('OK - ' + test);
callback();
}, (err) => {
console.log('ERR - ' + require('util').inspect(err));
callback(error.message);
}
);
}
});
}
错误示例:
update
save
Session token: r:c29b35a48d144f146838638f6cbed091
Master key: <my master key>
ERR- ParseError { code: 206, message: 'cannot modify user NPubttVAYv' }
如何正确保存我编辑过的用户?
How can I save correctly my edited user?
推荐答案
function login(logInfo, callback) {
let username = logInfo.email,
password = logInfo.password;
Parse.User.logIn(username, password).then(
(user) => {
if(!user) {
callback('No user found');
} else {
callback(null, user);
}
},
(error) => {
callback(error.message, null);
}
);
}
function update(userInfo, data, callback) {
login(userInfo, (error, user) => {
if(error) {
callback('Can t find user');
} else {
getUpdatedData(user.get('data'), data, (error, updateData) => {
if(error) {
callback(error);
} else {
user.save({data: updateData}, /*{useMasterKey: true}*/ {sessionToken: user.get("sessionToken")}).then(
(test) => {
callback();
}, (err) => {
callback(error.message);
}
);
}
});
}
});
}
出于某种原因,重新尝试使用 sessionToken
有效.
For some reason, retrying to use sessionToken
worked.
这篇关于解析服务器/JS SDK,保存用户对象时出现错误 206的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!