node.js - Using optional mongoose model property as URL param -


i using property of model url paramater '/parent/:arg1' , controller handles so

exports.parent = function (req, res) {   if (typeof(user.profile.property) == undefined) {     console.log("user not have property");   }   unirest('api.example.com/endpoint/' + user.profile.property)` 

however express returns connect 500 typeerror page saying cannot ready property 'property' of undifined (which expected) don't output typeof check end goal redirect user profile page update system.

what error telling user.profile undefined. error thrown because user.profile.property attempting access property of undefined. update conditional check like:

exports.parent = function(req, res) {   if (user.profile) {     if (user.profile.property === undefined) {       console.log("user.profile not have property");     } else {       unirest('api.example.com/endpoint/' + user.profile.property);     }   } else {     console.log("user not have profile");   } } 

Comments