Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/373.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.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发送后写入_Javascript_Mysql_Node.js - Fatal编程技术网

Javascript Node.js发送后写入

Javascript Node.js发送后写入,javascript,mysql,node.js,Javascript,Mysql,Node.js,我有2个mysql查询,1个用于检索我的GroupNumber,1个用于显示该组中的所有门。现在,当我检索我的组号时,我在数组中获取它们,这就是为什么我要在数组中映射它们 但是当我使用它时,我得到一个错误,发送后无法写入。有人知道我如何修复它 app.post("/door",function (req,res) { var door = req.body.door; Data(door,function (data) { res.writeHead(200

我有2个mysql查询,1个用于检索我的GroupNumber,1个用于显示该组中的所有门。现在,当我检索我的组号时,我在数组中获取它们,这就是为什么我要在数组中映射它们

但是当我使用它时,我得到一个错误,发送后无法写入。有人知道我如何修复它

  app.post("/door",function (req,res) {
    var door = req.body.door;

    Data(door,function (data) {
        res.writeHead(200, {'content-type': 'text/json' });
        res.write( JSON.stringify(data));
        res.end();
    });

});


function Data(door,next) {
    db.getGroups(function (e,groups) {
        if (e) console.log(e);
        else {
            async.map(groups,function (d) {
                db.getDoorD(d.GroupNumber,door,function (e,data) {
                    next(data);
                });
            });
        }
    });
}

map函数将迭代数组中的每个元素,因此第一个元素使用
res.end()
关闭响应,而当下一个元素尝试写入响应时,它无法完成

这里有几个选项,但我最喜欢的是使用库。看起来是这样的:

app.get("/door",function (req,res) {
db.getGroups(function (e,groups) {
    if(e) console.log(e);
    else
    {
        async.map(groups, db.getDoorD, function (err,results) {
               res.writeHead(200, {'content-type': 'text/json' });
               res.write( JSON.stringify(results));
               res.end();
           });
        });
    }
});
});
在这种情况下,
getDoorD
函数将发出一个回调,并对输入对象或数组进行转换,
results
数组将进行最终转换


您没有在这里发布SQL,但是也有一个很好的机会,一个更好的SQL查询将使您不必执行这里的N+1查询,而且性能更高

谢谢,但还是一样的问题,你没有遵循我的代码示例。请注意,正如我提到的,map的异步版本在迭代函数中有一个回调。每次调用它时,您必须将数据传递给下一个,您正在调用外部函数上的下一个,该函数每次都试图像以前一样调用相同的逻辑。