Parse Server / JS SDK, error 206 when saving a user object

你说的曾经没有我的故事 提交于 2020-07-18 16:50:07

问题


I am having trouble using the Parse Server JS SDK to edit and save a 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>.

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.

Here is my update function:

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);
                }
            );
        }
    });
}

And a exemple of the error:

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?


回答1:


I had the exact same problem when using Parse Server with migrated data from an existing app.

The app was created before March 2015 when the new Enhanced Sessions was introduced. The app was still using legacy session tokens and the migration to the new revocable sessions system was never made. Parse Server requires revocable sessions tokens and will fail when encountering legacy session tokens.

In the app settings panel, the Require revocable sessions setting was not enabled before the migration and users sessions were not migrated to the new system when switching to Parse Server. The result when trying to edit a user was a 400 Bad Request with the message cannot modify user xxxxx (Code: 206).

To fix the issue, I followed the Session Migration Tutorial provided by Parse which explain how to upgrade from legacy session tokens to revocable sessions. Multiple methods are described depending on your needs like enableRevocableSession() to enable these sessions on a mobile app, if you're only having a web app, you can enforce that any API requests with a legacy session token to return an invalid session token error, etc.

You should also check if you're handling invalid session token error correctly during the migration to prompt the user to login again and therefore obtain a new session token.




回答2:


I had the same error and neither useMasterKey nor sessionToken worked for me either. :( Here's my code:

        console.log("### attempt 1 sessionToken: " + request.user.getSessionToken());
        var p1 = plan.save();
        var p2 = request.user.save(null,  {sessionToken: request.user.getSessionToken()});
        return Parse.Promise.when([p1, p2]).then(function(savedPlan) {
        ...
        }

I see the matching session token in log output: 2016-08-21T00:19:03.318662+00:00 app[web.1]: ### attempt 1 sessionToken: r:506deaeecf8a0299c9a4678ccac47126

my user object has the correct ACL values:

"ACL":{"*":{"read":true},"PC7AuAVDLY":{"read":true,"write":true}}

I also see a bunch of beforeSave and afterSave logs with user being "undefined". not sure whether that's related.

beforeSave triggered for _User for user undefined:

I'm running latest parser-server version 2.2.18 on Heroku (tried it on AWS and results are the same)




回答3:


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);
                        }
                    );
                }
            });
        }
    });
}

For some reason, retrying to use sessionToken worked.




回答4:


This is not how asynchronous functions work in JavaScript. When createUser returns, the user has not yet been created. Calling user.save kicks off the save process, but it isn't finished until the success or error callback has been executed. You should have createUser take another callback as an argument, and call it from the user.save success callback.

Also, you can't create a user with save. You need to use Parse.User.signUp.

The function returns long before success or error is called.



来源:https://stackoverflow.com/questions/38564646/parse-server-js-sdk-error-206-when-saving-a-user-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!