gpt4 book ai didi

node.js - 如何在 Mongoose 中创建相互依赖的模式?

转载 作者:IT老高 更新时间:2023-10-28 23:26:59 25 4
gpt4 key购买 nike

我有两个 Schema,我希望它们能够相互交互。例如:

// calendar.js
var mongoose = require('mongoose');
var Scema = mongoose.Schema;
var Day = mongoose.model('Day');

var CalendarSchema = new Schema({
name: { type: String, required: true },
startYear: { type: Number, required: true }
});

CalendarSchema.methods.getDays = function(cb){
Day.find({ cal: this._id }, cb);
}

module.exports = mongoose.model('Calendar', CalendarSchema);


// day.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;
var Calendar = mongoose.model('Calendar');

var DaySchema = new Schema({
cal: { type: ObjectId, required: true },
date: { type: Number, required: true },
text: { type: String, default: 'hello' }
});

DaySchema.methods.getCal = function(cb){
Calendar.findById(this.cal, cb);
}

module.exports = mongoose.model('Day', DaySchema);

但是,我收到一个错误,因为每个架构都依赖于另一个架构。有没有办法使用 Mongoose 来完成这项工作?我像这样包含它们:

// app.js
require('./models/calendar');
require('./models/day');

最佳答案

我意识到这是一个古老的话题,但我确信发布解决方案会帮助其他人。

解决方案是在需要相互依赖的模式之前导出模块:

// calendar.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var CalendarSchema = new Schema({
name: { type: String, required: true },
startYear: { type: Number, required: true }
});

module.exports = mongoose.model('Calendar', CalendarSchema);

// now you can include the other model and finish definining calendar
var Day = mongoose.require('./day');
CalendarSchema.methods.getDays = function(cb){
Day.find({ cal: this._id }, cb);
}


// day.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;

var DaySchema = new Schema({
cal: { type: ObjectId, required: true },
date: { type: Number, required: true },
text: { type: String, default: 'hello' }
});

module.exports = mongoose.model('Day', DaySchema);

// same thing here. require after exporting
var Calendar = require('./calendar');

DaySchema.methods.getCal = function(cb){
Calendar.findById(this.cal, cb);
}

真的就是这么简单。 Brian Bickerton 的解释可以在这里找到:

http://tauzero.roughdraft.io/3948969265a2a427cf83-requiring-interdependent-node-js-modules

It's nice to be able to use functions by name within a module instead of the lengthy module.exports.name. It's also nice to have a single place to look and see everything to be exported. Typically, the solution I've seen is to define functions and variables normally and then set module.exports to an object containing the desired properties at the end. This works in most cases. Where it breaks down is when two modules are inter-dependent and require each other. Setting the exports at the end leads to unexpected results. To get around this problem, simply assign module.exports at the top, before requiring the other module.

关于node.js - 如何在 Mongoose 中创建相互依赖的模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12100356/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com