Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose findOne function not being executed

  ... (... = some unrelated code)

  var mongoose = require('mongoose');
  var db = mongoose.connect('mongodb://localhost/test');

  ...

  dataSchema = new Schema({
     'url': { type: String, index: true },
     'user_id': { type: Schema.ObjectId, index:true }
   });
  var Website = mongoose.model('websites', dataSchema);

  ...

  Website.findOne({url: "someurl.com"},function (err, docs) {
    console.log(docs._id);
  });

  ... 

For some reason the console.log does not execute. Is there anyway to tell if I am setting up my schema correctly or see if my find function failed or any sort of indication of where the problem might be? Currently, when I run my script, no errors occur, but nothing is printed out either.

Thanks!

like image 292
Bob Ren Avatar asked Sep 02 '12 22:09

Bob Ren


1 Answers

You can check for connection and schema errors by hooking the error event on the connection as:

mongoose.connection.on('error', function(err) {
    console.error('MongoDB error: %s', err);
});
like image 104
JohnnyHK Avatar answered Sep 20 '22 04:09

JohnnyHK