![](/img/trans.png)
[英]Node.js and mongodb TypeError: undefined is not a function
[英]TypeError: undefined is not a function - Node.js & MongoDB
由于某种原因,找不到在我的光标对象上调用的函数。 这是我的代码:
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)
有什么我想念的吗? 提前致谢。
myCursor
来自哪里?
使用cursor
而不是myCursor
:
var doc = cursor.hasNext() ? cursor.next() : null; //line 51
如果提供了find
的回调函数,则游标将仅提供给回调,而不会返回。 因此,删除console.log
参数,您将获得光标。
此外, next
不返回下一个文档,而是通过异步回调将其提供回调用方。
var cursor = collection.find({goal: selectedgoal});
if (cursor.hasNext()) {
cursor.next(function(err, doc) {
console.log(doc);
});
}
您确定错误行号吗?
我会说错误是从这些行来的:
var goalscollection = db.get('goalscollection');
var collection = db.get('hoursburnedcollection');
我找不到任何方法get
的驱动程序API。 应该是db.collection()
:
var goalscollection = db.collection('goalscollection');
var collection = db.collection('hoursburnedcollection');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.