Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Getting array of undefined, promise

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading