#Creeper - await and username

1 messages · Page 1 of 1 (latest)

fringe sable
#

That code gives me a headache 😄
And what's the error?

mortal plinth
#

well if the client.users.fetch() has an invalid id passed

#

then the whole app crashes

#

so i want to make the value null if it doesn't return a value

#

but i am not sure how to do that

fringe sable
#

So it's that the fetch errors, or returns undefined?

mortal plinth
#

yes the error is coming from the fetch

#

if i do fetch('123') i want it to return null but instead it crashes the whole app

fringe sable
#

You're 100% fetch itself is throwing an error, not that because it returns nothing, .username then errors?

#

Do you have the error trace?

mortal plinth
fringe sable
#

You sure the Id is a User Id, sounds like you made a magical Id

mortal plinth
#

i made the value an invalid id but i want to make it so it doesnt crash to program and instead it returns null

#

like the username variable should be equal to null

#

it used to work when i used to use client.users.cache.get() because that is not a promise and it just returns null if there is no value but then i noticed that i need to use .fetch() instead and now i get this error

fringe sable
#

In this case if you want to protect it from erroring, just move the username 1 out.

#

and put it in a try catch

#
let username;
try {
  username = (await ...)
} catch (error) {
  username = 'N/A';
}
mortal plinth
#

ty

#

that works

fringe sable
#

NP

mortal plinth
#

its just alot more lines than what i had before but thanks