Created
October 2, 2019 05:26
-
-
Save AbdelrahmanHafez/0dc7f7b9e9d75fae6895f99e5426b1cd to your computer and use it in GitHub Desktop.
Medium Optional Chaining Bad Usage Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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