Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AbdelrahmanHafez/0dc7f7b9e9d75fae6895f99e5426b1cd to your computer and use it in GitHub Desktop.
Save AbdelrahmanHafez/0dc7f7b9e9d75fae6895f99e5426b1cd to your computer and use it in GitHub Desktop.
Medium Optional Chaining Bad Usage Example
// on the user schema, the address, the city, and the name are all required
const user = await User.findOne(filter);
// the correct way of accessing the city name
const name = user.address.city.name;
// the way of accessing the name using optional chaining
const name = user?.address?.city?.name;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment