Home > Software design >  Getting value undefined in axios call
Getting value undefined in axios call

Time:10-10

I have following code calling post and get API calls.

Problem is I am getting customerDatas undefined..

Any idea why this issue is.

try {
        let dataPromises = await axios.post('customers', {}, { baseURL: baseURL });
    
        var customerDatas = await Promise.all(dataPromises.data.customerIds.map( async (customerId) =>{
            await axios.get('customers/'   customerId, {baseURL : baseURL});
            console.log(customerDatas);
        })); 
    } catch (error) {
        console.error(error);
    }

CodePudding user response:

As @Konrad Linkowski mention in the comments, you do not return anything from map:

var customerDatas = await Promise.all(dataPromises.data.customerIds.map( async (customerId) => {
  return axios.get('customers/'   customerId, {baseURL : baseURL});
}));

also, you cannot access customerDatas inside the map because it is not yet initalized.

  • Related