简体   繁体   中英

Using loop with async/await inside loop + nodejs

Hello in my nodejs api i need fetch data inside the loop and then again need to do a loop and save a data in another table how should i achive that? Here is some snippet that i have tried but not succeeded for the same

async myAPIname(){
    let _this = this;
    try {

        const bets = await Bet.find({gameId:ObjectId(request.matchId)}).lean()

        bets.map(async (bet) => {
            let Users  = await Users.findOne({_id:ObjectId(element.userId)}).lean();
            Users.parentTree.map(async (user) => {
                console.log(user);
                // also over here based on the some calculation need to save the data in another table
            })
        })

    } catch (error) {
        _this.res.send({ status: 0, message: error });
    }

}

Also in above snipped tried with foreach loop as well but not succeeded and error from above spinet like this:

(node:30886) UnhandledPromiseRejectionWarning: ReferenceError: Cannot access 'Users' before initialization
at /var/www/html/api/app/controllers/SomeController.js:228:30
at Array.map (<anonymous>)
at SomeController.myAPIname (/var/www/html/api/app/controllers/SomeController.js:227:18)
at processTicksAndRejections (internal/process/task_queues.js:97:5)

(node:30886) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)

(node:30886) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:30886) UnhandledPromiseRejectionWarning: TypeError: Assignment to constant variable.
    at /var/www/html/api/app/controllers/SomeController.js:220:27
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:30886) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)

Any Help will really appreciated

I can see two problems:

firstly the await inside a map call doesn't work like you think. It will work fine in a for-of loop but not in a map, foreach etc.

https://zellwk.com/blog/async-await-in-loops/ has a good an explanation as anywhere.

Secondly where you are calling let Users = Users.findOne, the compiler thinks that Users on the left hand side of the assignment is the same as the Users on the right hand side so it complains that when its calling Users.findOne, Users isn't initialised.

to use asynchronous handling with Array.prototype.map() you have to wrap it with Promise.all() and wait for it to fulfill.

!! however notice that iterations are executed in asynchronous way, not waiting for previous iteration to settle.

 const sleep = async (time = 3000) => new Promise(resolve => setTimeout(resolve, time)); (async () => { const array = [1,3,4,2]; console.log('start', array); const mapped =await Promise.all(array.map(async e => { await sleep(e * 1000); console.log(e); return `done for ${e}`; })); console.log('end', mapped); })();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM