0

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.

2
  • make sure your collection is not undefined Commented Aug 21, 2015 at 4:13
  • my collection is not undefined. the cursor is correct (i.e. contains the right documents) Commented Aug 21, 2015 at 4:19

3 Answers 3

1

From where myCursor comes?

Use cursor instead of myCursor:

var doc = cursor.hasNext() ? cursor.next() : null;  //line 51
Sign up to request clarification or add additional context in comments.

3 Comments

sorry, that's what i meant. even with the change, same error.
Which version of mongodb library you are using ?
The version i'm using is 2.0.33
0

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);
    });
}

1 Comment

This gives the same error (undefined is not a function) on cursor.hasNext(). It seems like its having trouble recognizing mongodb functions.
0

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');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.