简体   繁体   English

请求循环完成后如何获得响应

[英]How to get response after Loop of requests finished

I try to do multiple requests in for each loop and push data response to an array but I get always the first item only我尝试在每个循环中执行多个请求并将数据响应推送到一个数组,但我总是只得到第一个项目

//Get all roles
router.get('/', async (req, res) =>{

    try {

        knex.from( roles_table )
        .then(roles => {
            let rolePermissions =[];
            roles.forEach(role => {
                knex.select(permissions_table + '.*').from(permissions_table)
                    .innerJoin(permissions_roles_table, permissions_table + '.id', permissions_roles_table +'.id_permission')
                    .where(permissions_roles_table + '.id_role', '=', role.id)
                .then(rows => {
                    role.permissions = rows;
                    rolePermissions.push(role)
                });
                
            });
            
            res.status(200).json(rolePermissions);
            

           
        });
       
    } catch (err) {
        res.status(500).json({message: err});
    }
});

You must learn about Promise and asynchrone if you want to do modern JS.想做现代JS,必须要了解Promise和asynchrone。

Something like that could be what you are looking for类似的东西可能就是你要找的

//This const will store an array of Promise
const allRows = knex.from( roles_table )
.then(roles => {
    return roles.map(role => {
        return knex.select(permissions_table + '.*').[...]
    });
});

//This method will wait for each promises to succeed or first one to failed
return Promise.all(allRows)
.then( allRows => {
    //allRows is now an array containing all the results
    res.status(200).json(rolePermissions);
})
.catch (err) {
    res.status(500).json({message: err});
});

By the way, you don't need try/catch a Promise, there is the.catch method对了,不需要try/catch一个Promise,有.catch方法

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 可变大小循环内的http请求,只有在最后一个请求完成后才能做某事(JavaScript) - http requests inside loop of variable size, how to do something only after last request has finished (JavaScript) 仅在来自 axios 的请求的循环完成后执行代码块 - Execute block of code only after loop with requests from axios is finished HTTP请求循环完成后,将执行下一个功能 - After a loop of HTTP requests finished, next function will be executed 在异步循环完成之后,如何等待发送明确的响应? - How can I wait to send an express response until after async loop is finished? 循环结束后如何发送函数的结果? - How to send result of a function after the loop is finished? 在使用$ .get()请求进行多维for循环后,如何触发事件? - How to fire an event after a multi-dimensional for loop with $.get() requests? 如何在每次循环迭代之后而不是等到循环结束后才渲染组件? - How do I get a component to render after each loop iteration rather than waiting until the loop has finished? 如何在expressJs中完成实用程序执行后返回REST API响应 - How to return REST API response after utility execution is finished in expressJs 循环jQuery $ .get,如何知道循环何时完成 - Loop jquery $.get, how to know when loop is finished 如何在函数内的async.eachSeries循环内从多个get请求返回响应 - How do I return a response from multiple get requests, inside an async.eachSeries loop, inside a function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM