Klatch Baldar Klatch Baldar - 3 months ago 22
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

You could use _.get to make it safe:

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