【问题标题】:Model.find is not a function in mongooseModel.find 不是猫鼬中的功能
【发布时间】:2019-02-27 22:20:07
【问题描述】:

我是 node 和 mongodb 的新手。我正在尝试从另一个模型(公司)查询不同的模型(事件)。

基本上在Event 模型中有一个名为company 的字段。我想获取 id 是 Event ID 的公司。

我有一个数组中的所有事件 ID。

 let eventIds = [ 5b76a8139dc71a4a12564cd2,
  5b9a1685c239342d4635466c,
  5b8e753bdbccf803e906aaeb ]

事件架构 --

var EventSchema = new Schema({
        title:{type:String,require:true,index:true},
        description:{type:String,require:false},
        companies:[
            {type:Schema.Types.ObjectId,ref:"Company",require:true,index:true}
        ]
});

在公司模式中--

var mongoose = require('mongoose'),
    Schema = mongoose.Schema,
    Event = require('./event.js');

var CompanySchema = new Schema({
        name:{type:String,require:true,index:true},
        description:{type:String,require:false}},{
        //no auto indexing at the beginning
        autoIndex:true,

        //no strict to save changes in the valuesBeforeChange field.
        strict:false}
);

CompanySchema.static("searchCompanies",function(callback,criteria){

    "use strict";
    var That = this;
    var query = That.find();

    async.waterfall([

         function(callback){
             let eventIds =  [5b76a8139dc71a4a12564cd2,5b9a1685c239342d4635466c,5b8e753bdbccf803e906aaeb ];
             Event.find({ $in: eventIds}, function(err, docs){
                   console.log(docs);
             });
     }

],function(err,companyResultObj){
         callback(err,companyResultObj);
    });
});

我收到Event.find is not a function 错误消息。如何从另一个模型(公司)查询不同的模型(事件)

非常感谢任何帮助。

【问题讨论】:

  • 你能发布事件架构吗
  • @AnthonyWinzlet 当然。
  • @AnthonyWinzlet 我已经用事件模式更新了帖子。
  • 您缺少导出模型。添加此行export default Mongoose.model('Event', EventSchema)module.exports = Mongoose.model('Event', EventSchema)
  • @AnthonyWinzlet 现在得到 EventSchema is not defined 未定义。

标签: mongodb mongoose collections


【解决方案1】:

需要文件时使用别名

EventModel = require('./event.js');
then 
EventModel.find({ $in: eventIds}, function(err, docs){
   console.log(docs);
});

【讨论】:

  • 谢谢,但不工作。错误是EventModel.find is not a function
【解决方案2】:

您是如何导出 EventModel 的? 假设您将其作为模块导出(module.exports = { EventModel }), 你想去“const Event = require('./event.js').EventModel;”

然后简单地使用“Event.find(...”

【讨论】:

  • 如果有人遇到此特定解决方案有效的问题,则可能是您使用了错误的模型。使用导入的模型,而不是您创建新条目的模型。 const Shop = require('../models/shop'); const newShop = new Shop({..........}); newShop.find(); // won't work Shop.find(); // will work我希望这对某人有所帮助。
【解决方案3】:

不知道为什么,但我必须按以下方式执行此操作。

Event.find({ $in: eventIds}, function(err, docs){

mongoose.model('Event').find({_id:eventIds}, function(err, docs){

它返回了 3 个正确的文档。

【讨论】:

    【解决方案4】:

    这对我有用:

    在我的项目中,我将模型传递给一些中间件并开始看到这个问题,所以我使用了 mongoose.model('Name of Model')

    示例:.get(advancedResults(mongoose.model('Store'), 'stores'), getStores)

    【讨论】:

      【解决方案5】:

      如果您是从 index.js 或 index.ts 文件中导入模型,您有 最好将文件导入为:

      import YourModel from "./<file>/index"
      

      【讨论】:

        猜你喜欢
        • 2018-02-16
        • 2018-02-12
        • 2013-09-08
        • 2021-05-20
        • 1970-01-01
        • 2020-01-17
        • 2015-12-11
        相关资源
        最近更新 更多