node.js - Nested fields "Cannot read property 'zip' of undefined" -


having error. any idea how solve it?

object mongodb:

... "name" : true, "address" : {         "zip" : "bh9axx",         ... 

request db:

exports.getlist = function (req, res, next) {     user.find({         user: req.user.id     }, function (err, users) {         if (err) return next(err);          return res.render('list', {             users: users         });      }); }; 

list.jade:

each user in users     tr       td #{user.name}       td #{user.address.zip} 

everything works correctly, when #{user.address.zip} not used.

user.address undefined (at least in cases) , attempt access user.address.zip in jade file results in error.

possible solutions include:

  • perhaps address not required field in db schema or allowed undefined. if so, perhaps can make required.

  • you can add code before calling res.render() checks see if user.address undefined. if so, set default value (such object zip set empty string).


Comments

Popular posts from this blog

methods - python can't use function in submodule -

Java 3D LWJGL collision -

c# - ErrorThe type or namespace name 'AxWMPLib' could not be found (are you missing a using directive or an assembly reference?) -