1

Hi this is my function i am calling this from my controller like this: var getLastPoll = await socketModel. getPollOptionsByPollId(data.poll_id); but i am getting empty array as result in controller but when i logged result in my model it is returning the data of array containing two objects ? where i am doing mistake can someone help?

const getPollOptionsByPollId = async (poll_id) => 
{
    var querySql1 = "SELECT * FROM public.group_poll_options 

                     WHERE option_poll_id= $1 AND 

                    option_status = $2 ORDER BY option_id";

    var queryParams1 = [poll_id, "active"];
    var pollOptions = await db.query(querySql1, queryParams1);
    var result = [];
    if (pollOptions.rowCount != 0) {


        pollOptions.rows.map(async value => {
            var voteCount = await totalvotesbypolloption(poll_id, value.option_id);
            console.log("voteCount",voteCount);
            var data = {
                option_id: value.option_id,
                option_poll_id: value.option_poll_id,
                option_value: value.option_value,
                option_status: value.option_status,
                option_created_at: value.option_created_at,
                option_votes: voteCount
            }

            result.push(data);
        });

    }
    return result;
}```
3
  • (Tangential, but why use map only to push the data into an array? That seems redundant.) Commented Feb 13, 2023 at 19:29
  • @DaveNewton hi map is used because i want to count the option_votes for each option any other solution ? Commented Feb 13, 2023 at 19:35
  • map takes its input and creates a new array of the output. The only thing your usage does is populate data and push it into result--this is redundant; result = pollOptions.rows.map(... => { /* the rest */ return data }) would do the same thing. If you throw away the result of map then forEach would be more idiomatic (and incrementally less wasteful). Commented Feb 13, 2023 at 20:22

1 Answer 1

2

Using async/await when iterating over arrays with map, forEach, etc, doesn't do what you expect it to do. All iterations will finish before totalvotesbypolloption is called even once.

Replace map with with for...of:

    for (const value of pollOptions.rows) {
        var voteCount = await totalvotesbypolloption(poll_id, value.option_id);
        console.log("voteCount",voteCount);
        var data = {
            option_id: value.option_id,
            option_poll_id: value.option_poll_id,
            option_value: value.option_value,
            option_status: value.option_status,
            option_created_at: value.option_created_at,
            option_votes: voteCount
        }

        result.push(data);
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.