错误:发送标头后无法设置。在nodejs中

烈焰

我在应用程序中res.json(stat); // return in JSON format.遇到以下错误显示,第413行在下面的代码中指向“ ”,我查看了其他具有类似错误的帖子,但都没有帮助我,为什么我会遇到此错误?赞赏如何解决该错误的任何指导?

app.post('/api/botjobs.json', function(req, res) {
    // console.log(req.body)
    data = req.body;
    var temp = {};
    temp.radar = data.radar;
    temp.request = data.request;
    if(temp.request == 'cloneradar'){
        temp['params.clonetype'] = data.params.clonetype;
    }
    now = new Date();
    data["updated"] = now;      
    // console.log('DATA:')
    // console.log(data)
    // console.log('TEMP:')
    // console.log(temp)
    botjobs.findOne(temp, {}, { sort:{'_id': -1}}, function(err, findstat){
        // console.log(findstat)
        // console.log(err)
        // console.log("inside botjobs findOne");
        if(findstat != null){
            // console.log('updating entry')
            //console.log(findstat);
            data['numupdated'] = findstat['numupdated']+1;
            // console.log(data);
            botjobs.update({'_id':findstat['_id']},data,{ upsert : true, multi: true },function(err,stat){
                if (err)
                    res.send(err);

                res.json(stat); // return in JSON format
            });

        }
        else{
            // console.log('creating new entry')
            data["created"] = now;
            data['numupdated'] = 0;
            botjobs.create(data,function(err, stat) {
                // console.log(stat);
                // if there is an error retrieving, send the error. nothing after res.send(err) will execute
                if (err)
                    res.send(err);

                res.json(stat); // return in JSON format. —> line 413 in route.js
            });
        }

    });

});

错误:-

Error: Can't set headers after they are sent.
    at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:350:11)
    at ServerResponse.res.setHeader (/app/node_modules/express/node_modules/connect/lib/patch.js:134:22)
    at ServerResponse.res.set.res.header (/app/node_modules/express/lib/response.js:595:10)
    at ServerResponse.res.send (/app/node_modules/express/lib/response.js:151:12)
    at ServerResponse.res.json (/app/node_modules/express/lib/response.js:237:15)
    at /app/app/routes.js:413:10
不诚实

您可以添加这样的条件

if (err)
{
 res.send(err);
}
else
{
 res.json(stat);
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

NodeJs错误:发送标头后无法设置标头

错误:发送 Ember/Nodejs 后无法设置标头

错误:发送后无法设置标头。

NodeJS:发送标头后无法设置标头。仅在Heroku上发生错误

NodeJS / MongoDB-错误:发送标头后无法设置标头

快速重定向错误:使用NodeJS和MySQL发送标头后,无法设置标头

错误:发送标头后无法设置标头

UnhandledPromiseRejectionWarning:错误:发送标头后无法设置标头

收到错误-发送标头后无法设置标头

表达错误:发送标头后无法设置标头

如何修复Express中的“错误:发送标头后无法设置标头”

NodeJS-发送后无法设置标头-多次调用

发送标头后无法设置。Nodejs Express

使用POST的NodeJS Express身份验证发送后返回错误无法设置标头

Express - NodeJS - 错误:发送到客户端后无法设置标头

Nodejs multer 角度文件上传错误在发送后无法设置标头

收到错误:发送后无法设置标头

错误:发送标头后无法设置。节点Expressjs

ExpressJS“错误:发送后无法设置标头。”

错误:发送标头后无法设置。Connect.js

sailsjs策略导致发送标头后无法设置错误

带有节点的Express应用程序中的“错误:发送标头后无法设置标头”

航行错误:引发新错误(“发送标头后无法设置标头。”)

Socket.io导致错误“错误:发送标头后无法设置标头”。

错误:发送标头后无法设置标头-在Express Controller中正确处理错误

node.js/express 应用程序中的错误:发送后无法设置标头

收到“错误:发送标头后无法设置标头。” 进行多个API调用时

节点错误:发送标头后无法设置标头。怎么解决?

Express.js路由错误:发送标头后无法设置标头