TypeError:undefined 不是一个函数 - Node.js 和 MongoDB
2015-08-21
635
由于某种原因,找不到调用我的光标对象的函数。 这是我的代码:
var db = req.db;
var goalscollection = db.get('goalscollection');
var collection = db.get('hoursburnedcollection');
var cursor = collection.find({goal: selectedgoal}, console.log);
// the line above prints the correct cursor object
var doc = cursor.hasNext() ? cursor.next() : null; //line 51
// the line above gives an error
这是错误:
TypeError: undefined is not a function
at /Users/chrispark/Projects/LifeTool/node/routes/index.js:51:27
at Layer.handle [as handle_request] (/Users/chrispark/Projects/LifeTool/node/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/chrispark/Projects/LifeTool/node/node_modules/express/lib/router/route.js:131:13)
at Route.dispatch (/Users/chrispark/Projects/LifeTool/node/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/chrispark/Projects/LifeTool/node/node_modules/express/lib/router/layer.js:95:5)
at /Users/chrispark/Projects/LifeTool/node/node_modules/express/lib/router/index.js:277:22
at Function.process_params (/Users/chrispark/Projects/LifeTool/node/node_modules/express/lib/router/index.js:330:12)
at next (/Users/chrispark/Projects/LifeTool/node/node_modules/express/lib/router/index.js:271:10)
at Function.handle (/Users/chrispark/Projects/LifeTool/node/node_modules/express/lib/router/index.js:176:3)
at router (/Users/chrispark/Projects/LifeTool/node/node_modules/express/lib/router/index.js:46:12)
我是否遗漏了什么?提前致谢。
3个回答
myCursor
从何而来?
使用
cursor
代替
myCursor
:
var doc = cursor.hasNext() ? cursor.next() : null; //line 51
Hiren S.
2015-08-21
如果您为
find
提供回调函数,则光标将仅提供给回调而不会返回。因此,删除
console.log
参数,您将获得光标。
此外,
next
不会返回下一个文档,而是通过异步回调返回给调用者。
var cursor = collection.find({goal: selectedgoal});
if (cursor.hasNext()) {
cursor.next(function(err, doc) {
console.log(doc);
});
}
JohnnyHK
2015-08-21
您确定错误行号吗?
我会说错误是从这些行开始的:
258244091
我找不到任何方法
在驱动程序API中获取
。
它应该是
db.Collection()
:
616459571
codename44
2015-08-21