process.nextTick(function(){throw err;});错误:发送邮件后无法设置邮件

时间:2016-10-05 16:07:19

标签: javascript node.js mongodb express

我尝试在Node.js文件中从MongoDB获取数据 我收到此错误:

  

/home/jay/node_project/user_data_manag/node_modules/mongodb/lib/utils.js:98
  process.nextTick(function(){throw err;});

     

错误:发送后无法设置标头       在ServerResponse.OutgoingMessage.setHeader(_http_outgoing.js:335:11)
      在ServerResponse.header(/home/jay/node_project/user_data_manag/node_modules/express/lib/response.js:719:10)
      在ServerResponse.json(/home/jay/node_project/user_data_manag/node_modules/express/lib/response.js:247:10)
      在ServerResponse.send(/home/jay/node_project/user_data_manag/node_modules/express/lib/response.js:152:21)
      在/home/jay/node_project/user_data_manag/routes/api.js:42:13
      at /home/jay/node_project/user_data_manag/node_modules/mongodb/lib/cursor.js:777:7
      在handleCallback(/home/jay/node_project/user_data_manag/node_modules/mongodb/lib/utils.js:96:12)
      at /home/jay/node_project/user_data_manag/node_modules/mongodb/lib/cursor.js:741:16
      在handleCallback(/home/jay/node_project/user_data_manag/node_modules/mongodb/lib/utils.js:96:12)
      at /home/jay/node_project/user_data_manag/node_modules/mongodb/lib/cursor.js:675:5

// Dependencies
var express = require('express');
var router = express.Router();
var mongo = require('mongodb').MongoClient;
var objectId = require('mongodb').ObjectID;
var assert = require('assert');

var url = 'mongodb://localhost/rest_test';


router.post('/user', function(req, res, next) {
  if (req.body.name == "Login") {
    var result = [];
    mongo.connect(url, function(err, db) {
      var data = db.collection('products').find();
      data.forEach(function(doc, err) {
        result.push(doc);
      }, function() {
        db.close();
        res.send({
          iteamd: result
        });
      });
    });
  } else {
    res.send("data");
  }
  res.end();
});

//insert data
router.post('/put-data', function(req, res, next) {

  var item = {
    title: req.body.name,
    content: req.body.sku,
    author: req.body.price
  };
  var data = req.body.name;

  mongo.connect(url, function(err, db) {
    var cursor = db.collection('products').find({
      "name": data
    });
    res.send(cursor.toString);
  });

});


// Return router
module.exports = router;

2 个答案:

答案 0 :(得分:0)

请勿在同一个请求处理程序中调用res.send()res.end()

res.send()将为您致电res.end()source)。 res.end()通常在调用多个res.write()之后使用。

答案 1 :(得分:0)

如果多次调用 / user POST ,并且POST n 未关闭与 db.close的连接( )<; strong>在 n + 1 POST之前,您可能会收到相同的错误。

刚刚解决了我的剧本。