[英]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 开发环境
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.