Skip to content Skip to sidebar Skip to footer

Node-fetch Mapping Error - Cannot Read Property 'map' Of Undefined"

Getting an error with the 'map' part when I try and run it Cannot read property 'map' of undefined' The customers const is declared above so not sure. Where is the undefined is com

Solution 1:

getCustomers doesn't return anything which means that customers is set to undefined.

Try this:

async function getCustomers() {
  try {
    const resp = await fetch('https://3objects.netlify.com/3objects.json');
    const json = await resp.json();

    return json;
  }
  catch(e) {
    throw e;
  }
}

You also have to return something from the function that you pass as a parameter to .map

customers.map(async customer => {
    returnawait sendEmailToCustomer(customer);
});

or just:

customers.map(async customer => await sendEmailToCustomer(customer));

And since .map returns a new array (does not mutate the original array), you'll have to store the return value:

const customersEmailsPromises = customers.map(async customer => await sendEmailToCustomer(customer));

Post a Comment for "Node-fetch Mapping Error - Cannot Read Property 'map' Of Undefined""