Nic
Nic

Reputation: 501

Getting value undefined in axios call

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);
    }

Upvotes: 0

Views: 91

Answers (1)

MrBens
MrBens

Reputation: 1260

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.

Upvotes: 1

Related Questions