Mongoose findOne function not executing

... (... = 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, console.log is not running. Do I still have to say if I set up my circuit correctly or looked to see if the search function worked or any signs of where the problem might occur? Currently, when I run my script, no errors occur, but nothing is printed.

Thanks!

+7
source share
1 answer

You can check for connection and circuit errors by connecting an error event to the connection as:

 mongoose.connection.on('error', function(err) { console.error('MongoDB error: %s', err); }); 
+5
source

All Articles