0
我正在尝试将子文档导入到我的Express/Mongoose API中。似乎无法导入子文档
我有以下结构:
models
|-profile
| |-phones.js
|
|-profile.js
这里是phones.js模式文件:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var PhonesSchema = new mongoose.Schema({
phone_number:{ type: String },
phone_type:{ type: Number }
})
module.exports = mongoose.model('PhonesSchema', PhonesSchema);
这里是profile.js父文档,它驻留在模型文件夹:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var PhonesSchema = require('./profile/phones');
var ProfileSchema = new Schema({
//PROFILE INFO
owner_id: {
type: String,
require: true,
unique: true
},
linkedIn:{
type: String
},
phones:[PhonesSchema],
});
module.exports = mongoose.model('Profile', ProfileSchema);
但它不起作用。
任何想法?
你能告诉我你的目录结构? –
为什么要在模式中定义模式?只需将你的孩子定义为json对象... – LostJon
@LostJon,我的理解是这是一个最佳实践,因为它更像是OOP方法,并且允许我在其他场景中导入和重用模式块。我也认为它提供了一种更简洁的方法,可以让这些细分市场的增长更容易管理代码。你有什么想法,这种方法有什么缺点? – cnak2