Mongoose findOne函数未被执行

时间:2022-09-15 16:58:45
  ... (... = 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.

由于某种原因,console.log不会执行。无论如何要告诉我是否正确设置我的架构,或者查看我的查找功能是否失败,或者是否有任何迹象表明问题可能在哪里?目前,当我运行我的脚本时,没有错误发生,但也没有打印出来。

Thanks!

1 个解决方案

#1


5  

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);
});

#1


5  

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);
});