Invalid session token when attaching user to role

So, I’m trying to add an existing user to an existing role but I keep getting the Invalid session token error. I am using the master key for it though which is weird as I expected it to work.

Here is what I’m currently doing:

const roleQuery = new Moralis.Query(Moralis.Role);
roleQuery.equalTo("name", "testrole");
const role = await roleQuery.first({ useMasterKey: true });

const existingUserQuery = new Moralis.Query(Moralis.User);
existingUserQuery.equalTo("username", "testuser");
const existingUser = await existingUserQuery.first({ useMasterKey: true });

if (existingUser !== undefined) {
  role?.getUsers().add(existingUser);
  await role?.save(); // Also tried with save({useMasterKey: true}) but that didn't help.
}

When I log the role and user objects I see that it found them so I know it’s there and the master key worked otherwise I would not be able to see them.

Any idea what is going wrong here?

Here the syntax is .save(null, {})

Thanks, unfortunately that didn’t solve the problem.
I still get

ParseError: Invalid session token
    at handleError (/node_modules/moralis/lib/node/RESTController.js:423:17)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  code: 209
}

Nevermind, I forgot to add the useMasterKey.
This works:

await role?.save(null, { useMasterKey: true });

Thanks again :slight_smile:
I don’t know how much you get paid but you deserve a raise! :star2:

1 Like