I have a classes (or models) that needs to use another class as part of its properties as shown below.
我有一个类(或模型)需要使用另一个类作为其属性的一部分,如下所示。
** Header for both files **
**两个文件的标题**
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
item.js
module.exports = function() {
var ItemSchema = new Schema({
name: String,
cost: Number
});
mongoose.model('Item', ItemSchema);
}
receipt.js
ItemModel = require('./item.js');
var Item = mongoose.model('Item');
module.exports = function() {
var LineItemSchema = new Schema({
item: Item,
amount: Number
});
var LineItem = mongoose.model('LineItem', LineItemSchema);
var ReceiptSchema = new Schema({
name: String,
items: [LineItemSchema]
});
mongoose.model('Receipt', ReceiptSchema);
}
In the LineItem class, I'm trying to set the type of the variable 'item' to the class type, Item, node.js or mongoose.js is screaming at me about it saying that there's a type error.
在LineItem类中,我试图将变量'item'的类型设置为类类型,Item,node.js或mongoose.js正在尖叫着我说它有类型错误。
How can I use a Schema "type" from an external file?
如何使用外部文件中的Schema“type”?
2 个解决方案
#1
2
I have no idea why you are wrapping all of this in an anonymous function. But to reference a schema from another schema, you can do the following:
我不知道你为什么要在匿名函数中包装所有这些。但是要从另一个模式引用模式,您可以执行以下操作:
var LineItemSchema = new Schema({
item: {
type: Schema.ObjectId,
ref: 'Item'
},
amount: Number
});
And of course you need to require the Schema object:
当然,您需要需要Schema对象:
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
#2
0
In item.js
have it return the schema from a self-executing function.
在item.js中,它从自执行函数返回模式。
module.exports = (function() {
var ItemSchema = new Schema({
name: String,
cost: Number
});
mongoose.model('Item', ItemSchema);
return ItemSchema;
})();
Then in receipt.js
you now can use the schema just like you used LineItemSchema.
然后在receipt.js中,您现在可以像使用LineItemSchema一样使用模式。
var ItemSchema = require('./item.js');
// This should still create the model just fine.
var Item = mongoose.model('Item');
module.exports = function() {
var LineItemSchema = new Schema({
item: [ItemSchema], // This line now can use the exported schema.
amount: Number
});
var LineItem = mongoose.model('LineItem', LineItemSchema);
var ReceiptSchema = new Schema({
name: String,
items: [LineItemSchema]
});
mongoose.model('Receipt', ReceiptSchema);
}
This is all speculation and untested.
这是所有猜测和未经测试。
#1
2
I have no idea why you are wrapping all of this in an anonymous function. But to reference a schema from another schema, you can do the following:
我不知道你为什么要在匿名函数中包装所有这些。但是要从另一个模式引用模式,您可以执行以下操作:
var LineItemSchema = new Schema({
item: {
type: Schema.ObjectId,
ref: 'Item'
},
amount: Number
});
And of course you need to require the Schema object:
当然,您需要需要Schema对象:
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
#2
0
In item.js
have it return the schema from a self-executing function.
在item.js中,它从自执行函数返回模式。
module.exports = (function() {
var ItemSchema = new Schema({
name: String,
cost: Number
});
mongoose.model('Item', ItemSchema);
return ItemSchema;
})();
Then in receipt.js
you now can use the schema just like you used LineItemSchema.
然后在receipt.js中,您现在可以像使用LineItemSchema一样使用模式。
var ItemSchema = require('./item.js');
// This should still create the model just fine.
var Item = mongoose.model('Item');
module.exports = function() {
var LineItemSchema = new Schema({
item: [ItemSchema], // This line now can use the exported schema.
amount: Number
});
var LineItem = mongoose.model('LineItem', LineItemSchema);
var ReceiptSchema = new Schema({
name: String,
items: [LineItemSchema]
});
mongoose.model('Receipt', ReceiptSchema);
}
This is all speculation and untested.
这是所有猜测和未经测试。