Getting array of undefined, promise

Lazarusss

So, from code below im getting array of udefined but to be honest idk why and how to fix it...

const promises = sessionsIds.map((id) =>
            stripe.checkout.sessions
                .listLineItems(id, { limit: 100 })
                .then((err, lineItems) => {
                    return lineItems;
                })
        );

        const usernames = await Promise.all(promises);

I have also tried with async await but the result is still the same.

jfriend00

Assuming that stripe.checkout.sessions.listLineItems(id, { limit: 100 }) is returning a regular, standard promise, then the problem is that you're treating is like a regular asynchronous callback with (err, value) as the functions arguments. That's not how .then() works. The first argument to .then() is a function that will be called with one argument, the resolved value. In fact, you probably don't even need a .then() here.

You can change to this:

const promises = sessionsIds.map((id) =>
    stripe.checkout.sessions.listLineItems(id, { limit: 100 })
);

const usernames = await Promise.all(promises);

Or, simplifying further:

const usernames = await Promise.all(sessionsIds.map((id) =>
    stripe.checkout.sessions.listLineItems(id, { limit: 100 })
));

Note, the .then() handler here is not necessary (assuming stripe.checkout.sessions.listLineItems(id, { limit: 100 }) already returns a promise). If you want to use a .then() handler, it would go like this:

const promises = sessionsIds.map((id) =>
    stripe.checkout.sessions.listLineItems(id, { limit: 100 })
      .then((lineItems) => {
           console.log(lineItems);
           return lineItems;
      });
);

const usernames = await Promise.all(promises);

Where you pass it a function as the first argument and it gets one argument (the resolved value).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Promise getting resolved for undefined values

Getting an array from a promise

Array of object is getting undefined

Undefined 'this' in Promise + array. reduce()

Getting Error Promise is undefined in IE11

Second .then() on promise is getting called with data as undefined

Destructuring of an array of objects getting undefined

Getting undefined array after .map()

Why am I getting 'undefined' and empty array[] from a Promise.all call involving JavaScript async operations that use Azure APIs?

Promise.all is returning an array of undefined

JSON array undefined & empty in promise Bluebird

array is undefined when passed as argument in promise

Remove undefined resolved promise from array of promise in typescript

How to handle Promise.all properly: Getting undefined

why I'm getting array values as "undefined"

Getting undefined error while processing array

JavaScript — Array is getting filled with a bunch of "undefined"s

Getting undefined of inner array of JSON response in angular

Getting Undefined offset: 0 php array

Getting Undefined when taking values out of an array

getting undefined while displaying array on table

i keep getting Undefined array key

Trying to save object in useState but getting undefined or Array []

Why am i getting undefined by mapping an array?

undefined is not a promise

Promise is undefined

Getting promise undefined error when i use Promise in my word addin application

Promise.all() with map() returns undefined items in array

Promise.all() inside useEffect in react returns an array of undefined items