Klatch Baldar Klatch Baldar - 8 months ago 71
Node.js Question

Lodash isUndefined providing unexpected result

I have a ternary in Node that uses Lodash isUndefined:

sendUser.company = _.isUndefined(user.accounts[0].name) ? null : user.accounts[0].name;

but when I try to run it I get
TypeError: Cannot read property 'name' of undefined
. Not sure what I am missing here.


You could use _.get to make it safe:

sendUser.company = _.get(user, 'accounts[0].name')) || null;