skip to Main Content

I have a list of orders called listedOrders. In a loop I have to call myGetMemberFunction() which will return email of one member. After the loop is finished I have to put all the emails for all the members in a list and send it back in the response body.

myListOrdersFunction() and myGetMemberFunction() both return a promise that resolve to the desired data.
How do I return back the list of values. Do i use resolve or what

I am trying to return a list of emails whenever someone calls my API. (using wix-http-functions)

const myListOrdersFunction = require('backend/test').myListOrdersFunction;
const myGetMemberFunction = require('backend/getMember').myGetMemberFunction;

$w.onReady(function () {
    const emailList = myListOrdersFunction()
        .then(listedOrders => {
            const loginEmails = [];
            for (const order of listedOrders) {
                myGetMemberFunction(order.buyer.memberId)
                    .then(member => {
                        loginEmails.push(member.loginEmail);
                    })
                    .catch(error => {
                        console.log(error);
                    });
            }
            return loginEmails;
            
        })
        .catch(error => {
            console.log(error);
        });

        const printEmails = async () => {
            const a = await emailList;
            console.log("a ",a);
            //response.body = {
            //   "emails": loginEmails
            //};
            //return ok(response);
        }

        printEmails();
});

The problem is how do I return loginEmails in response.body. I understand that you have to implement a callback to get promise value. I have tried to do it but I don’t know where I am wrong. I have seen a lot of stack overflow answers on the same problem but I am not getting it. I am using a loop. I get all the listedOrders from myListOrdersFunction(). For all listedOrders I call the myGetMemberFunction in a loop. Every call returns an email. I want to make a list of these emails and send in back in the response body. Please help. I am at my wits end

2

Answers


  1. Chosen as BEST ANSWER

    Many thanks to everyone who helped me. I found the problem. Since I'm calling function in a loop it returns a promise for every call. So I use promise.all to get all the promises. Then I used another callback to get the return value. Thankyou very much


  2. I think this all can be simplified with just async/await

    const myListOrdersFunction = require('backend/test').myListOrdersFunction;
    const myGetMemberFunction = require('backend/getMember').myGetMemberFunction;
    
    $w.onReady(async function () {
        const listedOrders = await myListOrdersFunction()
        const members = await Promsie.all(listedOrders.map(order => myGetMemberFunction(order.buyer.memberId)
        const loginEmails = members.map(member => member.loginEmail)
        // then do whatever you want with the loginEmails
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search