How to get user Id in a Cloud Function?

I am trying to get the user’s id as a string.
I need to use id to do different validations.

I tried this at the top of my cloud function and I can pull other things about the user, but no id.

const user = request.user;
const userId = user.get('objectId'); //returns undefined ('id' as well)
const ethAddress = user.get('ethAddress'); //works

I found this thread, but I wasn’t able to follow the conclusion: Can not get user by objectId Moralis.User [SOLVED]

I can’t conceive a way to reliably query the user without id either. How can I do this?

I partially solved it with their weird code…

    if (!request.user) throw 'You must log in again.'
    const user = JSON.parse(JSON.stringify(request.user));
    logger.info(user.objectId); //got it!

request.user.id is probably enough

It’s weird but it spits a bunch of errors at me when I take off those JSON functions.

it looks like this works fine:

Moralis.Cloud.define('user_id', (request) => {
  return request.user.id
});

Hey! anybody know how to authenticate the user and get this user object when calling the cloud function by its http-endpoint?

Do you want to authenticate with your Moralis server? You can read this.

If you want to get a user object via REST/HTTP endpoint, you could pass something like the user’s wallet address, and use masterKey to find the object for that user.

Moralis.Cloud.define("getUser", async (request) => {
  const query = new Moralis.Query("_User");
  query.equalTo("ethAddress", request.params.ethAddress);
  const result = await query.first({ useMasterKey: true });

  return result;
});

And then call with https://serveridhere.usemoralis.com:2053/server/functions/getUser?ethAddress=addresshere

Yeah, useMasterKey:true was something that was missing also the query.first(). It worked thank you.