简体   繁体   English

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

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

So I have a very simple application that I'm building to practice with Mongo, Express, and Node. 因此,我有一个非常简单的应用程序,正在构建以与Mongo,Express和Node一起练习。

I'm getting an error process.nextTick(function(){throw err;}); 我遇到一个错误process.nextTick(function(){throw err;});

The error seems to occur when I try to use res.json(docs) in the success conditional during the GET request. 当我尝试在GET请求期间成功使用条件中使用res.json(docs)时,似乎发生了错误。 However, I can console.log(docs) and see the JSON. 但是,我可以console.log(docs)并查看JSON。

Development Environment 开发环境

  • Windows 7 64-bit Windows 7 64位
  • Mongo 3.2 蒙哥3.2
  • Node 5.6 节点5.6

package.json: 的package.json:

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

app.js (api): 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');

I'm not sure why this is failing. 我不确定为什么会失败。 I'm able to console log the JSON so know that the data is there and available in the response. 我能够控制台记录JSON,因此知道数据在那里并且在响应中可用。 Just not sure why the res.json(docs) doesn't work. 只是不确定为什么res.json(docs)不起作用。

You are calling the implicit callback twice 您两次调用隐式回调

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

and

res.json(docs);

remove either (probably the first one) 删除(可能是第一个)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 节点服务器未运行-抛出process.nextTick(function(){throw err;}) - node server not running - throwing process.nextTick(function() { throw err; }) process.nextTick(function(){throw err;}); 错误:发送标头后无法设置 - process.nextTick(function() { throw err; }); Error: Can't set headers after they are sent ReactJS + MongoDB + NodeJS / ExpressJS:什么是process.nextTick(function(){throw err;});? - ReactJS + MongoDB + NodeJS/ExpressJS: What is process.nextTick(function() { throw err; });? Node.js “类型错误:process.nextTick 不是函数”? - Node.js "TypeError: process.nextTick is not a function"? Node.js 异步函数,process.nextTick() - Node.js asynchronous function, process.nextTick() 如何解决process.nextTick错误? - How to solve process.nextTick error? process.nextTick 如何抛出“RangeError:数组长度无效” - How could process.nextTick throw "RangeError: Invalid array length" Node.js process.nextTick仍然阻止服务器获取请求 - Node.js process.nextTick still blocking server from getting requests 使用process.nextTick或setImmediate - node.js创建等待函数 - Creating a waiting function using process.nextTick or setImmediate - node.js Node.js 中 process.nextTick 的正确用例是什么? - What are the proper use cases for process.nextTick in Node.js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM