Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在Node.Js中用户选择forEach内部的查询?_Javascript_Jquery_Node.js_Foreach_Async Await - Fatal编程技术网

Javascript 如何在Node.Js中用户选择forEach内部的查询?

Javascript 如何在Node.Js中用户选择forEach内部的查询?,javascript,jquery,node.js,foreach,async-await,Javascript,Jquery,Node.js,Foreach,Async Await,这个问题已经被提出了,但这并不能解决我的问题 在我的Node.Js项目中,我希望用户在forEach中选择Query,但这不能正常工作 我参考了一些博客,他们说使用async和wait我试过了,但也不能正常工作 这是我的代码: db.sequelize.query(query, { type: sequelize.QueryTypes.SELECT} ).then(async dataList=>{ let cubbersIdList = []; // dataList =

这个问题已经被提出了,但这并不能解决我的问题

在我的Node.Js项目中,我希望用户
forEach
中选择Query
,但这不能正常工作

我参考了一些博客,他们说使用
async和wait
我试过了,但也不能正常工作

这是我的代码:

db.sequelize.query(query, { type: sequelize.QueryTypes.SELECT} ).then(async dataList=>{
    let cubbersIdList = [];
    // dataList = [{"cubbersId": 27},{"cubbersId": 28},{"cubbersId": 29}]
    await dataList.forEach((value, key)=>{
        CubbersShopsData.findAndCountAll({where:{cubbersId:value.cubbersId}}).then(datas=>{
            cubbersIdList.push(datas);
            console.log("---data---");
        }).catch(error=>{
            logger.error(error);
            res.status(200).send({status: 'error', resCode:403, msg:'Internal Server Error...!', data:error});
        });
    });
    console.log("---data---");
    res.send(cubbersIdList); // get result here
    console.log(cubbersIdList);
});
试试这个 安装npm

npm install async-foreach --save
添加到您的文件中

var forEach = require('async-foreach').forEach;
像这样使用forEach

db.sequelize.query(query, { type: sequelize.QueryTypes.SELECT} ).then(async dataList=>{
let cubbersIdList = [];
// dataList = [{"cubbersId": 27},{"cubbersId": 28},{"cubbersId": 29}]
forEach(dataList,function(value, key){
    var done = this.async();
    CubbersShopsData.findAndCountAll({where:{cubbersId:value.cubbersId}}).then(datas=>{
        cubbersIdList.push(datas);
        done();
    }).catch(error=>{
        done();
        logger.error(error);
        res.status(200).send({status: 'error', resCode:403, msg:'Internal Server Error...!', data:error});
    });
},function(err){
    res.send(cubbersIdList); // get result here
    console.log(cubbersIdList);
})

}))

尝试像这样重构代码,以便正确使用
async wait

在循环的for…中调用异步代码,等待承诺并解析响应,稍后将其推送到数组中

db.sequelize.query(query, { type: sequelize.QueryTypes.SELECT})
.then(async dataList => {
    let cubbersIdList = [];

    for (const data of dataList) {
        const count = await CubbersShopsData.findAndCountAll({where: { cubbersId: data.cubbersId }});
        cubbersIdList.push(count);
    }

    return res.status(200).json(cubbersIdList); 
})
.catch(err => res.status(500).json({ message: 'Some error occured!' }));

“工作不正常”是什么意思?你能更准确地描述一下预期的结果吗?产生的错误是什么?顺便说一下,当出现错误时,在“res.status…”之前添加一个返回occurs@Flo:wait not working,我的意思是首先运行这个代码
res.send(cubbersIdList)可能重复的@TGrif:不,这不是重复的如果你喜欢我的问题向上投票,我会给你3票向上投票你的答案我已经批准了你的D问题