节点服务器响应错误:process.nextTick(function(){throw err;});

所以我有一个非常简单的应用程序,我正在使用Mongo,Express和Node构build。

我得到一个错误process.nextTick(function(){throw err;});

当我尝试在GET请求中成功使用res.json(docs)时,似乎发生错误。 不过,我可以console.log(docs) ,看到JSON。

开发环境

  • Windows 7 64位
  • Mongo 3.2
  • 节点5.6

的package.json:

 ... "dependencies": { "body-parser": "^1.15.0", "express": "^4.13.4", "mongojs": "^2.3.0" } ... 

app.js(api):

 var express = require('express'); var app = express(); var mongojs = require('mongojs'); var db = mongojs('catalog', ['products']); app.get('/', function(req,res){ res.send('It works, indeed'); // this is working }); app.get('/products', function(req,res){ res.send('Products Works'); // this is displaying correctly on the page console.log('Fetching products'); db.products.find(function(err,docs){ if(err){ res.send(err); } else{ console.log('Sending Products'); console.log('response docs', docs); // returns the correct JSON res.json('stuff sent'); // Throws error } }); }); app.listen(3000); console.log('Server is running on port 3000'); 

我不知道为什么这是失败的。 我能够控制日志JSON,所以知道数据在那里,并在响应中可用。 只是不知道为什么res.json(docs)不起作用。

您正在调用隐式callback两次

 res.send('Products Works'); // this is displaying correctly on the page 

 res.json(docs); 

删除(可能是第一个)