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

Node server response error: process.nextTick(function(){throw err;});

本文关键字:throw err 服务器 响应 function process nextTick 节点 错误      更新时间:2023-09-26

所以我有一个非常简单的应用程序,我正在构建它来练习Mongo,Express和Node。

我收到错误process.nextTick(function(){throw err;});

当我尝试在 GET 请求期间以成功条件使用res.json(docs)时,似乎发生了错误。但是,我可以console.log(docs)并看到 JSON。

开发环境

  • 视窗 7 64 位
  • 蒙戈 3.2
  • 节点 5.6

package.json:

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

应用程序.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)不起作用。

您正在两次调用隐式回调

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

res.json(docs);

删除任何一个(可能是第一个)