For some reason, functions called on my cursor object are not found. Here's my code:
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
Here's the 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)
Is there something I'm missing? Thanks in advance.
From where myCursor
comes?
Use cursor
instead of myCursor
:
var doc = cursor.hasNext() ? cursor.next() : null; //line 51
If you provide a callback function to find
, the cursor will only be provided to the callback and not returned. So remove the console.log
parameter and you'll get the cursor.
Also, next
doesn't return the next doc, it provides back to the caller via an async callback.
var cursor = collection.find({goal: selectedgoal});
if (cursor.hasNext()) {
cursor.next(function(err, doc) {
console.log(doc);
});
}
Are you sure about the error line number ?
I would say the error is comming from those lines :
var goalscollection = db.get('goalscollection');
var collection = db.get('hoursburnedcollection');
I can't find any method get
in driver API. It should be db.collection()
:
var goalscollection = db.collection('goalscollection');
var collection = db.collection('hoursburnedcollection');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.