【问题标题】:can't set headers after they are sent return res.json发送后无法设置标头返回 res.json
【发布时间】:2016-10-21 19:18:53
【问题描述】:
exports.saveUserInterfaceConfig = function(req,res){
  var body = req.body;
  console.log('body:['+JSON.stringify(body)+']');
  var mysql = require('mysql');
  var UiConfigId = [];
  var connection = getDBConnection();
  if(body && connection){
    connection.beginTransaction(function(err){
      if (err) {
        /*var errorObj = {error:{code:0, text:'backend error'}};
         return res.json(200, errorObj);*/
        throw err;
      }
      var companyId = body.companyId;
      var moduleId = body.moduleId;
      var submoduleId = body.submoduleId;
      var formfieldsId = body.formfieldsId;
      for(var index3 in formfieldsId){
        var UIConfigInfo  = {Company_CompanyId: companyId, Modules_ModuleId: moduleId, SubModule_SubModuleId: submoduleId, SubmoduleFieldConfig_SubmoduleFieldConfigId: formfieldsId[index3]};
        var saveUIConfigQuery = 'INSERT INTO ui_config SET ?';
        connection.query(saveUIConfigQuery, UIConfigInfo, function(err, result) {
          if (err) {
            return connection.rollback(function() {
              throw err;
            });
          }
          UiConfigId.push(result.insertId);
          console.log('result:['+JSON.stringify(result)+']');
          connection.commit(function(err) {
            if (err) {
              return connection.rollback(function() {
                connection.end(function(err) {
                  // The connection is terminated now
                });
                throw err;
              });
            } else {
              connection.end(function(err) {
                // The connection is terminated now
              });
            }
            return res.json(200,{UiConfigId: UiConfigId});
            console.log('UiConfigId:['+JSON.stringify(UiConfigId)+']');
            console.log('success!');
//                          connection.release();
          });
        })

      }

    })
  }
}

我的 Node API 中有上述内容。我必须不止一次地在循环中执行相同的查询。但我在放置返回语句时遇到问题,我得到以下错误。

错误:发送后无法设置标头。 在 ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:335:11)

我该如何解决?

【问题讨论】:

    标签: mysql node.js api


    【解决方案1】:

    您在循环中多次调用 res.json,这就是您收到该错误的原因..

    简单来说,这种类型的错误会在你在发送响应后传递语句或其他东西时得到。

    例如:

    res.send("something response");
    console.log("jhgfjhgsdhgfsdf");
    console.log("sdgsdfhdgfdhgsdf");
    res.send("sopmething response");
    

    它会生成,你得到了什么错误。!! Beccoz 一旦发送了响应,下面的 res.send 将不会被执行..因为,我们每个请求只能发送一次响应。!! 为此,您需要使用回调。

    祝你好运

    【讨论】:

    【解决方案2】:

    您收到该错误的原因是您在循环中多次调用 res.json。

    首先,您应该使用回调机制在循环中执行查询。在其他查询完成之前执行多个查询可能会搞砸它。

    而响应,它也应该通过基于条件的回调来完成。条件可以是检查您是否已成功完成所有查询。

    这是一个页面,其中包含您所需要的详细信息: https://mostafa-samir.github.io/async-iterative-patterns-pt1/

    【讨论】:

      猜你喜欢
      • 2014-12-06
      • 1970-01-01
      • 1970-01-01
      • 2017-03-31
      • 2020-04-18
      • 2013-04-06
      • 1970-01-01
      相关资源
      最近更新 更多