mongoose keep having Error: unauthorized for a simple collection.find()

Trying for about 3 hours to translate a db.collection('collection-name').find() to mongoose but never seem to be able to auth the find().

Stack Trace:

Error: unauthorized db:parks ns:parks.parkpoints lock type:0 client:127.2.132.1
at Promise.error (/var/lib/user/app-root/runtime/repo/node_modules/mongoose/lib/promise.js:88:38)
at cb (/var/lib/user/app-root/runtime/repo/node_modules/mongoose/lib/query.js:1454:29)
at /var/lib/user/app-root/runtime/repo/node_modules/mongoose/lib/utils.js:408:16
at /var/lib/user/app-root/runtime/repo/node_modules/mongodb/lib/mongodb/cursor.js:146:30
at /var/lib/user/app-root/runtime/repo/node_modules/mongodb/lib/mongodb/cursor.js:188:32
at /var/lib/user/app-root/runtime/repo/node_modules/mongodb/lib/mongodb/cursor.js:504:39
at Cursor.close (/var/lib/user/app-root/runtime/repo/node_modules/mongodb/lib/mongodb/cursor.js:776:5)
at [object Object].<anonymous> (/var/lib/user/app-root/runtime/repo/node_modules/mongodb/lib/mongodb/cursor.js:504:21)
at [object Object].g (events.js:156:14)
at [object Object].emit (events.js:88:20)
npm info Sample-App@1.0.0 Failed to exec start script

Connection.once('open') event is hit, so connection shouldn't be the problem, but I keep hitting the error at console.log("find() error");.

Anyone has any clue on what's going on?

  • Database name: parks
  • Collection name: parkpoints
  • Mongoose version: 3.3.1
  • Mongodb version: 2.2.0

I double-checked the values for user, pass, host, and port, those shouldn't be the source of problem.

self.connectDb = function(callback){
mongoose.connection.on('error', function(err){
  console.log("connection error");
  throw err;});
mongoose.connection.once('open', function(){
  callback();
  self.Parkpoint=mongoose.connection.model('Parkpoint', 
           new Schema({ Name: String, pos: [Number]})); 

  self.Parkpoint.find().exec(function(err, names) {
    if(err){
      console.log("find() error");
      throw err};
     console.log("names: "+names);
    res.header("Content-Type:","application/json");
    res.end(JSON.stringify(names));
});


});

var dbUri = 'mongodb://'+self.dbUser+':'+self.dbPass+'@'+self.dbHost+':'+self.dbPort+'/parks';
console.log(dbUri);
mongoose.connect(self.dbHost, "parks", self.dbPort, {user:self.dbUser, pass:self.dbPass});
};

Resolved: need to run addUser() commands to add auth for the db. I assumed it working since mongodb driver worked, but apparently that user was never really added...maybe because it's admin. The root cause is still not very clear but the problem seems easy to solve with more attention to auth management.