Cloud Function cannot identify the user?

Hi, I’m following this tutorial, and was able to set up cloud function logs and IDE on visual studio.

However, I’m not sure why when I run a cloud function, it says that the user is undefined even though my client side app is able to run Moralis and find the user.

Here is my client side code

this.user =  Moralis.User.current();
this.user.id = this.user.get('ethAddress');
this.newService = await Moralis.Cloud.run("getService", {user:this.user.id});

Here is my cloud function

Moralis.Cloud.define("getService", async (request) => {
  const logger = Moralis.Cloud.getLogger();
  const query = new Moralis.Query("PlayerService"); 
  query.equalTo("user", request.params.user); 
  //query for moralis user, may need more complex checks, multiple ids eth, email, etc, correct?
  let results = {};
  let service = {};
  results = await query.find();
  //return the service object of that player

  //new player
  if (!results[0]) {
    logger.info(`change these results = ${results[0]}`);
    results = {
      service: {
        testProperty: false
      },
      user: request.params.user
    }
    const PlayerService = Moralis.Object.extend("PlayerService");  
    const playerService = new PlayerService();
    playerService.set(results);
    playerService.save();
    service = results.service;
  }
  else {
    service = results[0].get("service");
  }
  return service;
},{
  requireUser: true
});

Here is the Moralis Log

Failed running cloud function getService for user **undefined** with:
  Input: {"user":"0x07d9d8635bfcb19cc5d868df02a0af2addba31f5"}
  Error: {"message":"Validation failed. Please login to continue.","code":142}

Why is user undefined here?

(Also after I can identify the user on the cloud function, how can I reference that data there? I don’t want people being able to pass other people’s eth addresses)

You can use logger.info(JSON.stringify(request)) to see what you have there. You may also need to use master key for find and save.

This is what I get

{"params":{"user":"0x07*****5"},"master":false,"installationId":"9*****8","log":{"options":{"jsonLogs":false,"logsFolder":"./logs","verbose":false},"appId":"P*****t"},"headers":{"connection":"Upgrade","host":"127.0.0.1:1337","content-length":"198","accept-encoding":"gzip","cf-ipcountry":"US","x-forwarded-for":"2*****9","cf-ray":"6*****S","x-forwarded-proto":"https","cf-visitor":"{\"scheme\":\"https\"}","sec-ch-ua":"\"Chromium\";v=\"94\", \"Google Chrome\";v=\"94\", \";Not A Brand\";v=\"99\"","sec-ch-ua-mobile":"?0","user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36","sec-ch-ua-platform":"\"Windows\"","content-type":"text/plain","accept":"*/*","origin":"http://127.0.0.1:5500","sec-fetch-site":"cross-site","sec-fetch-mode":"cors","sec-fetch-dest":"empty","referer":"http://127.0.0.1:5500/","accept-language":"en-US,en;q=0.9","cf-connecting-ip":"2600:8801:*****29","cdn-loop":"cloudflare"},"ip":"2600:8801:*****29","functionName":"getService","context":{}}

Strange, same if you logout and authenticate again?

What is strange about it? I’m not sure what it’s suppose to look like.

I did log out and in several times. And it still happens. I wonder if it has anything to do with the “live server” plugin I’m using the preview this on VS code?

In an earlier stage, I was able to get the user though, so I figured it was something I messed up with the code.

I’ll test in an hour, my expectation is to have the user info in that request.

It looks like this works fine for me:

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

I’m still running into this problem :confused:

It’s pretty inconsistent. Sometimes I’m logged in and it works fine, sometimes, it just doesn’t see the user when running the cloud function.

Hmm when I authenticate on the page where I run the cloud function, it seems to work okay. It’s a single page application with react using react router dom for pages. I’ll have to test a bit more to find a cause.