vendredi 20 février 2015

MongoDB 2.6 + NodeJS can't iterate cursor

I really can't figure out what's wrong, please help.


MongoDB version 2.6.7


I can insert without any problem, however Find function preforms very wired, see below codes.


Code A works very well.



var mongoClient = require("mongodb").MongoClient;
var express = require("express");
var app = express();

mongoClient.connect("mongodb://localhost:27017/feeding", function(err,database){
if(!err){
db.collection('test').find({}).toArray(function(err, users){
console.log(users);
});
app.listen(8888, function(){
console.log("Server Started");
});
}else{
console.dir(err);
}
});


Below B code throws 'TypeError: undefined is not a function' at line 25



var mongoClient = require("mongodb").MongoClient;
var express = require("express");
var app = express();

mongoClient.connect("mongodb://localhost:27017/feeding", function(err,database){
if(!err){
var cursor = db.collection('test').find({});
while (cursor.hasNext()) { //line 25
console.log("printed");
}
app.listen(8888, function(){
console.log("Server Started");
});
}else{
console.dir(err);
}
});


I believe it says hasNext() is not a function, but refer to MongoDB document http://ift.tt/1LkX1sH, seems the code doesn't have any problem, have spent few hours on the issue. anybody can give me a suggestion?


Aucun commentaire:

Enregistrer un commentaire