Klatch Baldar Klatch Baldar - 10 months ago 78
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;