How to get phone number from User Account in Moralis using React Hooks?

Hi guys, I have two functions. One that is used to to insert a phone number (TEL) into Moralis _User collection on the Moralis Database and another function which is used to query data from it.

This is the one I use to insert data. It works perfectly.

  const updateTEL = async() => {
    const User = Moralis.Object.extend('_User');
    const query = new Moralis.Query(User);
    query.equalTo("ethAddress", account); 
    query.exists("TEL"); 
    const object = await query.first();
    object.set("TEL", phoneNumber.phone);
    object.save();
    console.log("phone number updated successfully" , object);
    return monster;
 }

Now I am having a problem with this function below.

 const queryTEL = async() => {
   if(isInitialized) {
  const User = Moralis.Object.extend('_User');
  const query = new Moralis.Query(User);
  query.equalTo("ethAddress", account);  
  const object = await query.first();
  if (object) { setExistingTEL(object.TEL);}
  return object;
   }
} 

const basicQuery = async () => {
  const results = await queryTEL();
  console.log (" results ", results);  
};

I don’t know why but it returns the result of ‘results’ as ‘undefined’.

Here is how my useEffect looks like.

  useEffect(() => {
      setVerificationCode(Math.floor(Math.random()*10000));
      basicQuery();  
}, []);

Am I doing something wrong?

Try removing this, maybe you are passing the wrong account address here.

Also you don’t need it as it only returns the logged in user row.

1 Like