Node.js 如何解决节点模型定义中的循环依赖?
我使用express和mongoose遵循MVC体系结构,我遇到了一个循环依赖的问题。代码本身是用ES6编写的 我记住了这两个特定的模型,我尽可能地模糊了这些模型: 目标模型,其中包含有关所有可用房间的信息: //destination.model.js 从“mongoose”导入mongoose,{Schema}; 从“./Booking.model”导入预订;//eslint在此检测依赖循环 从“./Room.model”导入房间; const DestinationSchema=新模式{ id:{type:Number,必需:true}, 名称:{type:String,必需:true,max:100}, 描述:{type:String,必需:false}, 房间:[Room.schema] }; DestinationSchema.statics.getAvailableRooms=异步函数startDate,endDate{ const bookings=等待预订。查找{'room.\u id':room.\u id}; //对这些预订做些什么 }; 导出默认mongoose.model'Destination',DestinationSchema; 以及预订模式,这与目的地的多对一相关 //booking.model.js 从“mongoose”导入mongoose,{Schema}; 从“./Destination.model”导入目标; 从“./Room.model”导入房间; const BookingSchema=新模式{ id:{type:Number,必需:true}, 客户端:{type:String,必需:true}, 开始日期:{类型:日期,默认值:}, endDate:{类型:日期,默认值:}, 文件室:{type:room.schema,必需:false}, 目的地:destination.schema }; 导出默认mongoose.model'Booking',BookingSchema; 主要问题: ESLint检测目的地模型和预订模型中的依赖循环(存在)。原因是,我在目的地模型中有一个静态方法,它查看所有预订,将来可能会调用一个静态方法 我的问题是,我该如何处理这个问题?我来自RubyonRails的背景,所以我非常习惯在一个模型中使用单个文件定义实例和静态方法 我不想将方法分离到另一个文件中,我想将它们保存在一个文件中——这有可能吗?或者我真的应该进行文件分离吗Node.js 如何解决节点模型定义中的循环依赖?,node.js,model-view-controller,mongoose,ecmascript-6,circular-dependency,Node.js,Model View Controller,Mongoose,Ecmascript 6,Circular Dependency,我使用express和mongoose遵循MVC体系结构,我遇到了一个循环依赖的问题。代码本身是用ES6编写的 我记住了这两个特定的模型,我尽可能地模糊了这些模型: 目标模型,其中包含有关所有可用房间的信息: //destination.model.js 从“mongoose”导入mongoose,{Schema}; 从“./Booking.model”导入预订;//eslint在此检测依赖循环 从“./Room.model”导入房间; const DestinationSchema=新模式{
干杯我认为您应该将预订模式建模为:
const BookingSchema = new Schema({
id: { type: Number, required: true },
client: { type: String, required: true },
startDate: { type: Date, default: '' },
endDate: { type: Date, default: '' },
room: { type: Room.schema, required: false },
destination: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Destination',
},
});
好的,这可能会解决这个特殊的问题-然而,如果在预订模型中有一个使用目的地模型的静态方法呢?是什么阻止了您这样做?