Node.js 错误:Can';t在发送邮件后设置邮件头。在节点js中

Node.js 错误:Can';t在发送邮件后设置邮件头。在节点js中,node.js,mongodb,Node.js,Mongodb,im在node js中是新的,当im试图从react向数据库发送数据时,在我的节点上,js出现错误“发送数据后无法设置头”。我一直在寻找类似于这个问题的答案,但它并不能帮助我解决这个问题 我试过在帖子上使用writeHead(第二个) 但这没用,是因为我发送了相同的图像吗?我只是在发送相同的图像时遇到问题 app.post('/list', function(req, res){ const {namaCabor, descCabor, imgCabor } = req.body;

im在node js中是新的,当im试图从react向数据库发送数据时,在我的节点上,js出现错误“发送数据后无法设置头”。我一直在寻找类似于这个问题的答案,但它并不能帮助我解决这个问题

我试过在帖子上使用writeHead(第二个) 但这没用,是因为我发送了相同的图像吗?我只是在发送相同的图像时遇到问题

app.post('/list', function(req, res){
    const {namaCabor, descCabor, imgCabor } = req.body;
    connectDb.collection('listCabangOlahraga').insertOne(req.body, function(err, res){
        console.log(res.insertedCount +'data inserted');
    });
    res.send(req.body);
});

app.post('/add', function(req, res){
    const {categoryName, categoryDesc, categoryImage, namaCabor, descCabor, imgCabor } = req.body;
    connectDb.collection('listCategoryCabangOlahraga').insertOne(req.body, function(err, res){
        console.log(res.insertedCount +'data inserted'); 
        if(err) throw err;
    });
    res.writeHead(200, {'Content-Type' : 'application/json'});
    res.end(JSON.stringify(req.body));
});

我知道您有一个错误处理程序。在/add路由中,它抛出一个错误,您的错误处理程序将捕获它。所以您尝试在/add处理程序和错误处理程序中设置响应头。第二个处理程序将抛出错误“发送头后无法设置头,因为响应已在第一个处理程序中发送(可能是错误处理程序或/添加处理程序)

快速分析:您的代码涉及写入MongoDB集合。它有一个异步回调。我想res.write()/res.send()应该包含在回调中吗

如果没有,它们甚至在DB操作完成之前就被执行,我们不知道它是否成功

app.post('/list', function(req, res){
    const {namaCabor, descCabor, imgCabor } = req.body;
    connectDb.collection('listCabangOlahraga').insertOne(req.body, function(err, res){
        console.log(res.insertedCount +'data inserted');
        // <----- Handle the error here and print response accordingly.
    });
    res.send(req.body); //Move this inside callback. Return error response if err encountered.
});

app.post('/add', function(req, res){
    const {categoryName, categoryDesc, categoryImage, namaCabor, descCabor, imgCabor } = req.body;
    connectDb.collection('listCategoryCabangOlahraga').insertOne(req.body, function(err, res){
        console.log(res.insertedCount +'data inserted'); 
        if(err) throw err;
        // <----- Handle the error here and print response accordingly.
    });
    res.writeHead(200, {'Content-Type' : 'application/json'}); // Move this inside callback.
    res.end(JSON.stringify(req.body)); //Write response from the callback.
});
app.post('/list',函数(req,res){
const{namaCabor,descCabor,imgCabor}=req.body;
connectDb.collection('listCabangOlahraga').insertOne(请求主体、函数(err、res){
log(res.insertedCount+'data inserted');

//每当节点在发送http响应后尝试发送http响应时,就会发生此错误, e、 g

但是,在您的情况下,在您发布的代码中没有可见的此类场景, 但是在server.js中,执行的方法可能已将响应发送到前端,并且在路由控制器完成处理之前池已关闭,从而导致代码中的res.send或res.end抛出错误


因此,请尝试检查代码库的其他部分以调试此错误。

在第一篇文章中,它可以正常工作,但在另一篇文章中,这意味着('/add'),当我从前面键入数据并输入类型文件(在我的例子中是它的图像)时总是有错误,不能设置标题,但是如果我只是在那里输入文本,它工作得很好,为什么可能呢?而且我刚刚更改了代码,正如你所说的,但是当我上传图像文件时仍然存在问题。你能共享控制台的输出吗?当它出错时,当它不出错时?错误:发送后不能设置标题。在validaServerResponse.setHeader(_http_outing.js:491:11)ServerResponse.header(D:\backend cabor\node\u modules\express\lib\response.js:767:10)ServerResponse.send(D:\backend cabor\node\u modules\express\lib\response.js:170:12)的ServerResponse.setHeader(_http_outing.js:491:498:498:3)(D:\backend-cabor\node\u modules\express\lib\response.js:267:15)在ServerResponse.send(D:\backend-cabor\node\u modules\express\lib\response.js:158:21)在D:\backend-cabor\app.js:20:21,这是错误,但如果数据正常,则只发送“插入1个数据”
{if(true){
res.send({})
}
res.send({
// this will throw that error because the pool is already close because the first res.send was executed.
});
}