Jessie Anderson
Jessie Anderson

Reputation: 319

map is undefined if object is undefined

I have this in my jsx

{get(applicant, 'user.name').map((obj,i) => <span>{obj}</span>}

if user.name not exist, my app will crash, why get doesn't solve the problem here? It's lodash's get.

If I do applicant.user.name.map my app will have chance to crash.

Upvotes: 2

Views: 436

Answers (1)

Mikhail Shabrikov
Mikhail Shabrikov

Reputation: 8509

Lodash _.get return undefined if path doesn't exist. As result undefined.map throws the error. You should pass [] as the third argument, in that case, if path is not exist _.get returns [] and [].map does not throws the error:

{get(applicant, 'user.name', []).map((obj,i) => <span>{obj}</span>}

Upvotes: 1

Related Questions