Klatch Baldar Klatch Baldar - 2 years ago 145
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.

Answer Source

You could use _.get to make it safe:

sendUser.company = _.get(user, 'accounts[0].name')) || null;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download