ReferenceError: collection is not defined (?!?)

4,739 views
Skip to first unread message

Manchuwook

unread,
Dec 21, 2011, 12:28:21 PM12/21/11
to node-mongodb-native
Fairly simple code, annoying problem. I'm looking at the collection
dead in the face for MongoExplorer - it has a single entry in it - and
yet for some reason Nodejs can't seem to find it. Authenticate
returns with a true, so I am not having the problem with
authentication (i.e. "username" and "password" works just fine).

var m = require("mongodb");
var msrv = new m.Server("localhost", 27017, {});
db = new m.Db('tinhalo', msrv);
db.open(function (error, client) {
db.authenticate("username", "mypass", function (err, val) {});

db.collection("players", function(err, cursor){
collection.find(function (err, cursor){
cursor.each(function(err, doc){
if(doc != null) console.dir(doc);
})
})
})
});

ubuntu@may:/srv/node/tinhalo$ sudo node ./site.js

node.js:134
throw e; // process.nextTick error, or 'error' event on first
tick
^
ReferenceError: collection is not defined
at /srv/node/tinhalo/site.js:10:3
at [object Object].collection (/home/ubuntu/.node_libraries/.npm/
mongodb/0.9.7-2-3/package/lib/mongodb/db.js:287:12)
at /srv/node/tinhalo/site.js:9:5
at /home/ubuntu/.node_libraries/.npm/mongodb/0.9.7-2-3/package/lib/
mongodb/db.js:161:14
at Object.callback (/home/ubuntu/.node_libraries/.npm/mongodb/
0.9.7-2-3/package/lib/mongodb/connection/server.js:148:7)
at /home/ubuntu/.node_libraries/.npm/mongodb/0.9.7-2-3/package/lib/
mongodb/connection/server.js:253:32
at [object Object].parseBody (/home/ubuntu/.node_libraries/.npm/
mongodb/0.9.7-2-3/package/lib/mongodb/responses/mongo_reply.js:110:5)
at [object Object].<anonymous> (/home/ubuntu/.node_libraries/.npm/
mongodb/0.9.7-2-3/package/lib/mongodb/connection/server.js:242:24)
at [object Object].emit (events.js:64:17)
at [object Object].<anonymous> (/home/ubuntu/.node_libraries/.npm/
mongodb/0.9.7-2-3/package/lib/mongodb/connection/connection_pool.js:
153:13)

Manchuwook

unread,
Dec 21, 2011, 12:32:13 PM12/21/11
to node-mongodb-native
Forgot to add:

ubuntu@may:/srv/node/tinhalo$ npm list
bs...@0.0.3 active installed
coffee...@1.1.3 active installed
col...@0.5.1 active installed
comm...@0.2.1 active installed
con...@1.8.1 active installed
connec...@0.4.1 active installed
exp...@2.5.1 active installed
fl...@1.0.5 active installed
formi...@1.0.8 active installed
ho...@0.1.9 active installed
ja...@0.18.0 active installed
mi...@1.2.4 active installed
mkd...@0.0.7 installed
mkd...@0.2.1 active installed
mon...@0.9.6-23 installed
mon...@0.9.7-1.3 installed
mon...@0.9.7-2-3 active installed
mong...@2.3.13 installed
mong...@2.4.0 active installed
oa...@0.9.5 active installed
ope...@0.3.1 installed
ope...@0.3.2 active installed
pdf...@0.1.6 active installed
q...@0.4.0 active installed
npm ok

Hernan Silberman

unread,
Dec 21, 2011, 1:01:26 PM12/21/11
to node-mong...@googlegroups.com
db.open is passing you a db connection (your 'client') and you're not
using it. ask it for the collection instead and it should work.

--
ngmoco:)
hsilb...@ngmoco.com
415-606-1312

christkv

unread,
Dec 21, 2011, 1:28:09 PM12/21/11
to node-mong...@googlegroups.com
another thing is a typo

 db.collection("players", function(err, cursor){ 

should be

 db.collection("players", function(err, collection){ 

Manchuwook

unread,
Dec 21, 2011, 1:48:51 PM12/21/11
to node-mongodb-native
so something more like this:

var m = require("mongodb");
var msrv = new m.Server("localhost", 27017, {});

db = new m.Db('tinhalo', msrv);
db.open(function (error, client) {
db.authenticate("myuser", "mypass", function (err, val) {});
var coll = client.collection("staff",function(err, collection)
{ });
});

coll.find(function (err, cursor){
cursor.each(function(err, doc){
if(doc != null) console.dir(doc);
})
})

?

Hernan Silberman

unread,
Dec 21, 2011, 2:04:34 PM12/21/11
to node-mong...@googlegroups.com
https://gist.github.com/1507229

The examples folder in the driver project has lots of useful examples like this.

https://github.com/christkv/node-mongodb-native/tree/master/examples

--
ngmoco:)
hsilb...@ngmoco.com
415-606-1312

Manchuwook

unread,
Dec 21, 2011, 2:32:52 PM12/21/11
to node-mongodb-native
Gist gives me this, after modding to add authentication (I have to
have authentication):

var util = require('util');
var m = require("mongodb");

var msrv = new m.Server("localhost", 27017,
{ auto_reconnect:true,poolSize:1,safe:true });
var db = new m.Db('tinhalo', msrv);

db.open(function(err,connection) {
db.authenticate("myuser", "mypass", function (err, val)
{ if(err) throw new Error(err); });
connection.collection('staff',function(err,collection) {
if(err) throw new Error(err);
collection.find({},function(err,cursor) {
cursor.toArray(function(err,items) {
if(err) throw new Error(err);
console.log('FOUND:
'+util.inspect(items));
});
});
});
});

ubuntu@may:/srv/node/tinhalo$ sudo node ./site.js

node.js:134
throw e; // process.nextTick error, or 'error' event on first
tick
^
Error: unauthorized db:tinhalo lock type:-1 client:127.0.0.1
at /srv/node/tinhalo/site.js:16:47
at /home/ubuntu/.node_libraries/.npm/mongodb/0.9.7-2-3/package/lib/
mongodb/cursor.js:129:30
at /home/ubuntu/.node_libraries/.npm/mongodb/0.9.7-2-3/package/lib/
mongodb/cursor.js:173:32
at /home/ubuntu/.node_libraries/.npm/mongodb/0.9.7-2-3/package/lib/
mongodb/cursor.js:471:67
at [object Object].close (/home/ubuntu/.node_libraries/.npm/
mongodb/0.9.7-2-3/package/lib/mongodb/cursor.js:679:5)
at Object.callback (/home/ubuntu/.node_libraries/.npm/mongodb/
0.9.7-2-3/package/lib/mongodb/cursor.js:471:21)
at /home/ubuntu/.node_libraries/.npm/mongodb/0.9.7-2-3/package/lib/
mongodb/connection/server.js:253:32
at [object Object].parseBody (/home/ubuntu/.node_libraries/.npm/
mongodb/0.9.7-2-3/package/lib/mongodb/responses/mongo_reply.js:110:5)
at [object Object].<anonymous> (/home/ubuntu/.node_libraries/.npm/
mongodb/0.9.7-2-3/package/lib/mongodb/connection/server.js:242:24)
at [object Object].emit (events.js:64:17)

And I have been going through the examples, but they drop either the
db or the collection before doing anything else... which confuses me
if I want to use existing data. What I am shooting for is to tie this
to MVVMC with Jade, so I have to take baby steps. This has been a big
hangup for me after days of searching. Of course, the next question I
am going to ask is how to take the cursor.toArray and pass it up the
chain so I can view it in a webpage.
> hsilber...@ngmoco.com
> 415-606-1312

christkv

unread,
Dec 21, 2011, 3:03:25 PM12/21/11
to node-mong...@googlegroups.com
most likely the user is set up not correctly on the db. the tests you are interested in are under

test/auxiliary

Cheers

Manchuwook

unread,
Dec 21, 2011, 3:14:13 PM12/21/11
to node-mongodb-native
I looked at the tests like you suggested and it definitely clears up
some confusion, thanks! However, I'm not sure how you mean the user
is not correctly setup... I'm not throwing an exception on the
authenticate, only on the next line. Do I have to do collection-level
user access? I already granted that user access to the db, perhaps
something on the addUser in mongo?

Manchuwook

unread,
Dec 21, 2011, 4:14:09 PM12/21/11
to node-mongodb-native
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF.... /headdesk

var util = require('util');
var m = require("mongodb");

var msrv = new m.Server("localhost", 27017, { auto_reconnect:true,
poolSize:1, safe:true });
var db = new m.Db('tinhalo', msrv);

db.open(function(err,conn) {
console.log("Database Opened");
db.authenticate("myuser", "mypass", function (err, val)
{ if(err) { throw new Error(err)} else
{ console.log("Authenticated"); }
conn.collection('staff',function(err, coll) {
if(err) throw new Error(err);
coll.find({ }, function(err, cursor)
{ cursor.toArray(function(err, items) {
if(err) throw new Error(err);
console.log('FOUND: ' +
util.inspect(items));
cursor.each( function(err,doc)
{ if(doc != null) { console.log(doc); } })
}); });
});
});
});

Can't run db.authenticate(...); conn.someOtherStuff();

Has to happen INSIDE authenticate(...(err,conn) { /* HERE */ })

Thank you all for the help! This has gone a long way.

Manchuwook

unread,
Dec 21, 2011, 7:01:12 PM12/21/11
to node-mongodb-native
So, last question: I now have an array of items - how do I pass them
to another function, like app.get?

christkv

unread,
Dec 22, 2011, 5:53:27 AM12/22/11
to node-mong...@googlegroups.com
I guess you are pretty new to node.js :)

I have a very simple application that you can look at for a start


But you should look in the express github repo. It's got lots of examples on how to organize your code using different approaches.

Also use something like 

async or step for orchestrating your calls (running stuff in parallel, serially etc). Just look them up on github.

Welcome aboard mate.
Reply all
Reply to author
Forward
0 new messages