Skip to content Skip to sidebar Skip to footer

Unable To Perform .map Whithin Function

Background I'm building a React-Redux application which, amongst other things, has to handle some axios calls to a private API. Said API is not under my control, and I cannot modif

Solution 1:

The problem is that you map your values to a list of Promises.

However, .then() is not callable on a list ([Promise, Promise, ...]) in your case. In fact, .then will be undefined which is exactly what the error message tells you.

Instead, try to wrap values.map(...) in a Promise.all(...). This will wait for all promises to resolve before calling .then(...).

Keep in mind that the response passed to .then(...) will actually also be a list!

So you want to do something like this:

Promise.all(
  values
    .map(value =>
      apiInstance.request({
        url: ENDPOINTS.ENDPOINT,
        method: 'POST',
        data: qs.stringify({
         token: token,
         category: value,
        }),
      }),
    )
)
.then(responses => {
  // responses is a list!return responses.map(response => response.data);
})
.catch(error => {
  returnPromise.reject(error.message);
});

Solution 2:

try this code

exportconstgetData = (tk, values) =>
  values
    .map(value =>
      apiInstance.request({
        url: ENDPOINTS.ENDPOINT,
        method: 'POST',
        data: qs.stringify({
         token: token,
         category: value,
        }),
      })
      .then(response => {
        return response.data;
      })
      .catch(error => {
        returnPromise.reject(error.message);
      })
    );

Post a Comment for "Unable To Perform .map Whithin Function"