[SOLVED] Cloud function can't find user by ethAddrress

I’m calling a Cloud function from one of my API routes and I’m having an issue where it’s not finding the ethAddress of user. I’m trying to switch a user’s ā€œemailVerifiedā€ value but getting "error": "user.set is not a function" returned from the route.

It looks like .find() isn’t matching anything but it should.

The logger results below:
Logs.. shows:

Logs...{"params":{"_ApplicationId":"XPL7i7UbHjQQINxEFpY8eRLk7LM2EtdoyF8Vdhdq","ethAddress":"0x9396c0fed2af8257f59437f461de1c8ff7b066301","changeVerified":"true","unsetVerifyToken":"true"},"master":false,"functionName":"switchUserVerified","context":{}}

ethAddress... returns the correct address

User | Query: {"where":{"ethAddress":"0x9.......01"}}
User: []

Not sure where I’m going wrong here?

Moralis.Cloud.define('switchUserVerified', async (request) => {
  const logger = Moralis.Cloud.getLogger();

  logger.info('Logs...' + JSON.stringify(request));
  logger.info('ethAddress...' + JSON.stringify(request.params.ethAddress));

  const User = Moralis.Object.extend('_User');
  const query = new Moralis.Query(User);
  query.equalTo('ethAddress', request.params.ethAddress);
  logger.info(`User | Query: ${JSON.stringify(query)}`);

  const user = await query.find({ useMasterKey: true });
  logger.info(`User: ${JSON.stringify(user)}`);

  user.set('emailVerified', changeVerifiedBool);
  user.unset('verifyToken');

  logger.info('Saving user...');
  await user.save(null, { useMasterKey: true });
});

This is how I’m calling the Cloud function from Next.js API route:

const response = await fetch(
          `https://46664sqdmacoh.usemoralis.com:2053/server/functions/switchUserVerified?_ApplicationId=${appId}&ethAddress=${address}&changeVerified=${changeVerifiedBool}&unsetVerifyToken=${changeVerifiedBool}`,
          {
            method: 'POST',
          }
        );
const data = await response.json();

this may return an array, you may also have to use lowercase for the address

Ya it returns an empty array but it should be finding my user.

Not sure why its not working.

if you try it without that equalTo then it works as expected as in to return all the users?

@cryptokid - Ya it does find all

can you try by searching by another field, like username, just to see if it works?

@cryptokid - yup using the username works

maybe there are some invalid rows in that table for ethAddress?

oh man, sorry, I had a number 1 added in the route in Postman after the address. Must’ve been a typo.

Thanks for helping!

1 Like

@cryptokid - actually the query looks good now but when it gets to user.set() or user.save()` it’s returning:

"data": {
  "code": 141,
  "error": "user.save is not a function"
}

"data": {
  "code": 141,
  "error": "user.set is not a function"
}
user.set('emailVerified', false); 
await user.save(null, { useMasterKey: true });

Any idea?

user has to be an object, not an array

Oh right. Thanks for your help!