我正在研究node.js中的一个简单Web服务。我正在使用编排器来路由http调用。此代码正常工作:将Node.js参数传递给mongodb请求
router.get('/search/*', function(req, res, term){
res.writeHead(200, {'Content Type:':'text/plain'});
db.collection('foo').find({'a':1}).toArray(function(err, items){
console.log(items);
res.write(JSON.stringify(items));
res.end();
});
});
正如您所看到的,find方法查找{'a':1},这工作正常,返回一条记录。但是,当我想从路由器通过搜索项的查询,我得到一个空的回应:
router.get('/search/*', function(req, res, term){
res.writeHead(200, {'Content Type:':'text/plain'});
db.collection('foo').find({'a':term}).toArray(function(err, items){
console.log(items);
res.write(JSON.stringify(items));
res.end();
});
});
任何想法吗?
编辑:我已经检查了术语的值,如下面的评论中所建议的那样,它是1,这是我预期的结果。
你检查了术语的价值吗? console.log(term) – simplyharsh
simplyharsh,是的,我有,这是预期的 – 2bard
你检查了err的值吗?如果执行查询有任何问题,则err将为非null,并且'items'为空。经常检查是很好的。 – mpobrien