继续获取错误:发送后无法设置标头

时间:2017-05-18 01:28:45

标签: node.js mongodb

所以我正在尝试更新代码并将其显示给用户。基本上它能够做我需要做的事情但是在执行之后我得到了这个错误

  

C:\用户\ tester01_2 \ myproject的\ node_modules \ mongodb的核\ lib中\ cursor.js:174         扔错了;         ^       错误:发送后无法设置标头。       在ServerResponse.OutgoingMessage.setHeader(_http_outgoing.js:357:11)       在ServerResponse.header(       C:\ Users \用户tester01_2 \ myproject的\ node_modules \表达\ lib中\ response.js:725:10)       在ServerResponse.send       (C:\ Users \用户tester01_2 \ myproject的\ node_modules \表达\ lib中\ response.js:170:12)       在C:\ Users \ tester01_2 \ myproject \ dbUpdate.js:13:14       在C:\ Users \ tester01_2 \ myproject \ dbUpdate.js:28:5       在handleCallback(C:\ Users \ tester01_2 \ myproject \ node_modules \ mongodb-       芯\ lib中\ cursor.js:171:5)       在nextFunction(C:\ Users \ tester01_2 \ myproject \ node_modules \ mongodb-       芯\ lib中\ cursor.js:682:5)       在Cursor.next [as _next]       (C:\ Users \用户tester01_2 \ myproject的\ node_modules \ mongodb-       芯\ lib中\ cursor.js:692:3)       在循环       (C:\ Users \用户tester01_2 \ myproject的\ node_modules \ mongodb的\ lib中\ cursor.js:694:8)       在每一个       (C:\ Users \用户tester01_2 \ myproject的\ node_modules \ mongodb的\ lib中\ cursor.js:741:16)

这是我的代码

var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');
var url = 'mongodb://localhost:27017/myproject';

module.exports = {

postCollection : function(req, res){
    var issueQty = req.body.issueQty;
    var itemDescrip = req.body.itemDescrip;
MongoClient.connect(url, function(err, db) {
    assert.equal(null, err);
    updateRecord(db, req, function(doc) {
    return res.send('Record Found. Now updating this document...' + 
itemDescrip + ' Record Updated. This is the new record ' + doc )
    res.end();
    db.close();
    });

});
}
}
var updateRecord = function(db, req, callback) {
var cursor = db.collection('documents').find({'Item Description': 
req.body.itemDescrip, 'Issued QTY': req.body.issueQty})
cursor.each(function(err,doc){
   assert.equal(err, null);
     if(doc != err){
         console.log('Successfully queried');
         console.log(doc);
         callback(JSON.stringify(doc));

     } else{
         throw err;
     }
  });
db.collection('documents').updateMany(
  { 'Item Description': req.body.itemDescrip},
  {
    $set: { 'Issued QTY': req.body.issueQty }
  },function(err, results) {
    console.log(results);
    console.log('Done');
    console.log(results);

});
};

我认为这与我的res有关,因为我看到所有线程都处于错误的位置,但我需要将res.send放在那里,以便它可以使用doc。有什么方法可以解决这个问题吗?谢谢。

1 个答案:

答案 0 :(得分:0)

首先,您应该删除return前面的res.send()

其次,在updateRecord函数中,callback函数不应该在循环中调用,它将执行多次。并在执行updateMany之前关闭数据库。

如果要发送doc,则应使用临时数组来保存doc,并在完成updateRecord中的所有逻辑时将其传递给res。

如果我理解正确,以下是我对您的代码的修改,

var MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://localhost:27017/myproject';

var updateRecord = function(db, req, callback) {
    db.collection('documents').updateMany({ 'Item Description': req.body.itemDescrip }, {
        $set: { 'Issued QTY': req.body.issueQty }
    }, function(err, results) {
        if (err) return callback(err);
        console.log('Done');
        console.log(results);
        var cursor = db.collection('documents').find({
            'Item Description': req.body.itemDescrip,
            'Issued QTY': req.body.issueQty
        });
        var temp = [];
        cursor.each(function(err, doc) {
            if (err) {
                return callback(err);
            }
            console.log('Successfully queried');
            console.log(doc);
            temp.push(JSON.stringify(doc));
        });
        callback(null, temp);
    });
};

module.exports = {
    postCollection: function(req, res) {
        var issueQty = req.body.issueQty;
        var itemDescrip = req.body.itemDescrip;
        MongoClient.connect(url, function(err, db) {
            if(err) {
              res.send(err);
              res.end();
              db.close();
              return;
            }
            updateRecord(db, req, function(err, docs) {
                if(err){
                  res.send(err);
                }
                else{
                  res.send(docs);
                }
                res.end();
                db.close();
            });

        });
    }
}