Answer

The error occurs because `user` is `undefined` when you try to access `user.profile.name`. This can happen if the query `db.findOne` returns `null` when no document is found. To safely access nested properties, use optional chaining (`?.`) and default values. Update your code like this: ```js const user = await db.findOne({ id: userId }); console.log(user?.profile?.name ?? 'Default Name'); ``` This prevents the error by returning `undefined` if any part of the chain is missing, and provides a fallback value.

f15fd129-3d45-48a3-a360-4c2aa2f0dafa

The error occurs because user is undefined when you try to access user.profile.name. This can happen if the query db.findOne returns null when no document is found. To safely access nested properties, use optional chaining (?.) and default values. Update your code like this:

const user = await db.findOne({ id: userId });
console.log(user?.profile?.name ?? 'Default Name');

This prevents the error by returning undefined if any part of the chain is missing, and provides a fallback value.