1

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 118
SIGABRT: 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)
2
  • If you don't want to change your unit tests, you need to check your input values for this function. How do you call this function? Commented Oct 25, 2021 at 13:38
  • this is obvious) But you are probably passing an undefined value. So the error should be looked for in the code of the component that calls this function. You can also try to change the function signature as in the second option - const memberDropdownOptions = (members: Person[]): Option[]. In this case, the error will be clearer Commented Oct 25, 2021 at 13:46

1 Answer 1

1

The members argument that you are passing to memberDropdownOptions(members) is not defined when the function is being called.

Is members relying on some asynchronous logic to be given a value? Trace back to where it's being assigned a value, and make sure memberDropdownOptions() only fires once it does have one.

Sign up to request clarification or add additional context in comments.

2 Comments

So do you recommend that instead of changing any of the function code I should just change the usage to let memberOptions: Option[] = [] // Check if members is undefined if (members) memberOptions = memberDropdownOptions(members)
Hard to say without seeing the rest of the code, but personally, when making a function, I would never allow an opportunity for the argument to be undefined. Look at your parameter (members: Person[]). The type is Person[], NOT Person[] | undefined. Change your code outside of this function to ensure that your argument is never undefined. Does TypeScript not throw an error saying that members may be undefined? If not, you might have incorrectly built your type/interface. Just keep going up the chain, console logging so you can find out where it is not being assigned.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.