How to use object of type Promise <string | null> as a string

868 views Asked by At

hello guys I am having a problem with the types in typescript. I am using micro orm to fetch data from the database I am using this code

const user = (await orm).em.findOne(User, { username: options.username });

it's returning me

user: Promise<string | null>

my problem starts here I have to use this object as a string in the form

user.password 

in following query

const valid = await argon2.verify(user.password ,options.password);

but its giving me error as

Property 'password' does not exist on type 'Promise<User | null>'

I have no idea how to resolve this

1

There are 1 answers

2
Aadmaa On

It looks like you are already in an async function. The issue is that you are not waiting for the user promise to complete.

Try this:

// Check that this is really giving you an Entity Manager
const em = (await orm).em;

const users = await em.findOne(User, { username: options.username }); 
if (!users || users.length === 0) throw new Error('No user!');
const user = users[0];
const valid = await argon2.verify(user.password ,options.password);