[SOLVED] Moralis Cloud function with useMasterKey returns invalid session token

I ran some basic Moralis.Cloud.define(...) based functions that return constant values and were using Moralis.Logger.getLogger that worked well. Also accessing public collections works well.

However, as soon as I try to access something using useMasterKey I receive an error message about the session token.

The cloud function is

    const logger = Moralis.Cloud.getLogger();
    logger.info('TestFunction: Querying roles...')
    const query = new Moralis.Query(Moralis.Role);
    const result = await query.find(null, {useMasterKey: true})
    logger.info('TestFunction: -> [x] Completed request')
    return `TestFunction: (Stamp ${(new Date()).toISOString()})`
2022-06-20T06:07:53.302Z - Error: Invalid session token
    at new RestQuery (/moralis-server/lib/RestQuery.js:66:13)
    at /moralis-server/lib/rest.js:35:19
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
2022-06-20T06:07:53.296Z - Failed running cloud function testme for user 09j... XXXX...IpFFrxtPOZ with:
  Input: {}
  Error: {"message":"Invalid session token","code":209}
2022-06-20T06:07:53.293Z - TestFunction: Querying roles...

Thanks for any help!

EDIT: Using query.find({useMasterKey: true}) solved my error message.

1 Like