Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

Map Function - Unhandled JS Exception: TypeError: undefined is not an object evaluating map

$
0
0

I have a file called displayKeyHelpers.ts and the members.map code is on line 118. The code works for most users and in QA as well but occasionally it seems members is undefined. I am looking at a crash report that says

displayKeyHelpers.ts, line 118SIGABRT: Unhandled JS Exception: TypeError: undefined is not an object (evaluating 'n.map') This error is located at: in V in Unknown in Unknown in Unknown..

It seems like members in the function below is undefined. How can I add a check?

export const memberDropdownOptions = (members: any): Option[] => {  const options: Option[] = []  members.map((member: Person) => {    options.push({      label: `${member.firstName} ${member.lastName}`,      value: member.dependentNumber,    })  })  return options}

This is what I tried but it failed some unit tests so I am guessing that it is not right:

export const memberDropdownOptions = (members: Person[]): Option[] => {  const options: Option[] = []  members?.map((member: Person) => {    options.push({      label: `${member?.firstName} ${member?.lastName}`,      value: member?.dependentNumber,    })  })  return options}

The function is used like this:

const memberOptions = memberDropdownOptions(members)

Viewing all articles
Browse latest Browse all 6287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>