DrakaSAN DrakaSAN - 3 months ago 65
Javascript Question

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

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?

Answer
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.

Comments