Model.find不是猫鼬中的函数 [英] Model.find is not a function in mongoose
问题描述
我是node和mongodb的新手.我正在尝试从另一个模型(公司)查询另一个模型(事件).
I am new in node and mongodb. I am trying to query a different model(Event) from another model(Company).
基本上,在 Event
模型中,有一个名为 company
的字段.我想获得ID为事件ID的公司.
Basically in Event
model there is a field called company
. I would like to get the company where id is an Event ID.
我将所有事件ID放在一个数组中.
I have all the event IDs in an array.
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不是函数
错误消息.如何从另一个模型(公司)查询另一个模型(事件)
I am getting Event.find is not a function
error message. How can I query a different model(event) from another model(company)
我们非常感谢您的帮助.
Any help is highly appreciated.
推荐答案
不知道为什么,但是我必须按照以下方式进行.
Not sure why but I had to do this in the following way.
Event.find({ $in: eventIds}, function(err, docs){
收件人
mongoose.model('Event').find({_id:eventIds}, function(err, docs){
返回了3个正确的文档.
which returned 3 documents which are correct.
这篇关于Model.find不是猫鼬中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!