Cannot read property of type null with async map

map is the javascript method of the array object, so you need to check if array is not null before call map method.

In your example, you can’t guarantee that testauth is always array, and it can be null value.

const asyncFunc = async () => {
  const arr = []

  testauth && testauth.map(e => {
      arr.push(e.value)
      console.log('pushed value',arr);
    })
};

FYI, the way you used map is the the correct way. map returns an transformed array with the same length of the input array. You need to use as follows:

const asyncFunc = async () => {
  const arr = testauth? testauth.map(e => e.value) : []
};

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top