Sniberb
Sniberbβ€’2y ago

πŸŽƒ Spookyberb πŸŽƒ – 13-03 Nov 23

Will this piece of code crash the app if it doesnt find anything?
const { fullName } = clients.find((client) => client.clientId === customerId);
const { fullName } = clients.find((client) => client.clientId === customerId);
7 Replies
Unknown User
Unknown Userβ€’2y ago
Message Not Public
Sign In & Join Server To View
ScriptyChris
ScriptyChrisβ€’2y ago
@β›„Snowberbβ›„ add a default value if .find() returns null:
const { fullName } = clients.find((client) => client.clientId === customerId) ?? { fullName: '' };
const { fullName } = clients.find((client) => client.clientId === customerId) ?? { fullName: '' };
Sniberb
Sniberbβ€’2y ago
ooo I like this that's what I wanted but why ?? and not ||?
Unknown User
Unknown Userβ€’2y ago
Message Not Public
Sign In & Join Server To View
ScriptyChris
ScriptyChrisβ€’2y ago
Alternatively you could use optional chaining without spread πŸ€”
const fullName = clients.find((client) => client.clientId === customerId)?.fullName;
const fullName = clients.find((client) => client.clientId === customerId)?.fullName;
!mdn optional chaining
reactibot
reactibotβ€’2y ago
This thread hasn’t had any activity in 12 hours, so it’s now locked. Threads are closed automatically after 12 hours. If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/565213527673929729/1044961281167331379 Question not getting answered? Maybe it's hard to answer, or maybe you asked at a slow time. Check out these resources for help asking a good question: https://stackoverflow.com/help/how-to-ask http://wp.me/p2oIwo-26