Search code examples
typescriptobjectundefined

Typescript: Object property returns "undefined"


I was working on a function and I want it to check the username for signin API call. My code are as below:

export const signIn = async (AuthUser: IAuthUser) => {
  try {

    const userData = await execute<IAuthUser>(AuthQueries.SignIn, [AuthUser.email]);

    console.log(userData.email);  // undefined
    console.log(userData); // log object data {user1email.com, password_for_user1}

    return userData;

  } catch (error) {
    throw error;
  }
};

So the question is why userData.email returns undefined, while userData has values? And how to fix that?


Solution

  • Solved. I realised that userData is getting result from MySQL query using the "mysql2": "^2.3.3" package and the result is a RowDataPacket object. This way, I can access the userData values by index.

    For example:

    console.log(userData[0].email); //returns the actual value