由于某种原因,找不到在我的光标对象上调用的函数。 这是我的代码:
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)
有什么我想念的吗?提前谢谢。
答案 0 :(得分:1)
myCursor
来自哪里?
使用cursor
代替myCursor
:
var doc = cursor.hasNext() ? cursor.next() : null; //line 51
答案 1 :(得分:0)
如果向find
提供回调函数,则光标将仅提供给回调而不返回。因此,请删除console.log
参数,然后您将获得光标。
此外,next
不会返回下一个文档,它会通过异步回调提供给调用者。
var cursor = collection.find({goal: selectedgoal});
if (cursor.hasNext()) {
cursor.next(function(err, doc) {
console.log(doc);
});
}
答案 2 :(得分:0)
您确定错误行号吗?
我会说错误来自这些方面:
var goalscollection = db.get('goalscollection');
var collection = db.get('hoursburnedcollection');
我在驱动程序API中找不到任何方法get
。
它应该是db.collection()
:
var goalscollection = db.collection('goalscollection');
var collection = db.collection('hoursburnedcollection');