Home > Software design >  Typescript: Object property returns "undefined"
Typescript: Object property returns "undefined"

Time:07-01

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?

CodePudding user response:

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

For example:

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