简体   繁体   English

猫鼬查找查询未执行

[英]Mongoose find query is not executing

I am trying to fetch some data from a mongodb. 我试图从mongodb获取一些数据。 I am able to fetch the details from the model MyModel (defined in Server.js). 我能够从模型MyModel (在Server.js中定义)中获取详细信息。 But I can't execute the find method in the model Skill (defined in Skill.js). 但是我无法在Skill模型(在Skill.js中定义)中执行find方法。

Server.js

  let express = require('express');
  let app = express();
  var config = require('./config/config');
  let bodyParser = require('body-parser');
  var mongoose = require('mongoose');
  var cors = require('cors')
  var path = require("path");
  let Skill = require('./app/models/Skill');
  const dbURI = config.dbURI;
  app.use(cors());
  app.use(bodyParser.json(true));
  mongoose.connect(dbURI, {useNewUrlParser: true});
  mongoose.connection.on('connected', function () {
         console.log("Connected");
  }); 
  var MyModel = mongoose.model('Test', new Schema({ name: String 
  }));
  Skill.findOne(function(error, result) { 
           console.log("1",error,result);
  });
  MyModel.findOne(function(error, result) { 
           console.log("2",error,result); 
  });
  app.listen(config.appPort,function () {
          console.log('App running on port :: "' + config.appPort.toString() + '"');

  });

app/models/Skill.js

 var mongoose = require('mongoose');

 var skillSchema = new mongoose.Schema({
       name: String,
       length: String,
 });

 var Skill = mongoose.model('Skill', skillSchema,'Skill');
 module.exports = Skill; 

Output

   App running on port :: "8080"
   Conneccted
   2 null { _id: 5c6678215c50a65e59fc6a89, name: 'test', __v: 0 }

I haven't found any issues while creating the schema. 创建架构时我没有发现任何问题。 Could someone help me to resolve the issue? 有人可以帮我解决问题吗?

I can't test my suggestion at the moment, but one thing I noticed in your code is that you queries, as in 我目前无法测试我的建议,但是我在您的代码中注意到的一件事是您查询,例如

Skill.findOne(function(error, result) { 
       console.log("1",error,result);
});
MyModel.findOne(function(error, result) { 
        console.log("2",error,result); 
});

are not executed in the "connected" callback; 没有在“连接”回调中执行; may be it's not the main problem, but I would try to change the code as follows: 可能不是主要问题,但是我将尝试按以下方式更改代码:

mongoose.connection.on('connected', function () {
    console.log("Connected");
    Skill.findOne(function(error, result) { 
       console.log("1",error,result);
    });
    MyModel.findOne(function(error, result) { 
        console.log("2",error,result); 
    });
});

The key thing is that each call ( .on , .findOne ) starts a promise , which encapsulates an asynchronous behaviour. 关键是每个调用( .on.findOne )都会启动一个promise ,该promise封装了一个异步行为。 I you place ,findOne invocation just after .on invocation, you can't be sure Mongoose's connection is ready when you starts .findOne ... 我对你的地方,findOne刚过调用.on调用,你不能肯定猫鼬的连接已经准备好,当你开始.findOne ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM