【发布时间】:2016-11-06 11:43:53
【问题描述】:
当我需要保存“文档”时,我遇到了循环依赖问题。
我的应用程序都是通过 AngularJS 接口的 Restfull。
在创建公司的屏幕上,您可以创建对象和服务。在创建SERVICES屏幕时,必须关联一个已创建的OBJECT。
问题是创建的 OBJECT 还没有持久化,所以我没有 _id。 因此无法在 SERVICE 中引用它。只有当我有一个 OBJECT 持久化时,我才能将它关联到 company.services[0].object。
有什么建议吗?
这是我需要保存在 MongoDB 中的。查看 SERVICE 中对 OBJECT "5779f75a27f9d259248211c7" 的引用。
/* 1 */
{
"_id" : ObjectId("5749bb92bf8145c97988e4a9"),
"name" : "company 1",
"services" : [
{
"_id" : ObjectId("5764cb2c00d00cf10c9c41c6"),
"description" : "service 1",
"object" : ObjectId("5779f75a27f9d259248211c7"),
}
],
"objects" : [
{
"_id" : ObjectId("5779f75a27f9d259248211c7"),
"description" : "object 1",
}
]
}
这是我的架构:
var objectSchema = new mongoose.Schema({
description: {
type: String,
trim: true,
unique: true,
required: 'Description is required'
}
})
var serviceSchema = new mongoose.Schema({
description: {
type: String,
trim: true,
required: 'Description is required'
},
object: {
type: mongoose.Schema.ObjectId,
ref: 'Object'
},
})
var companySchema = new mongoose.Schema({
name: {
type: String,
default: '',
trim: true,
unique: true,
required: 'Name is required'
},
guarda_documentos: {
services: [serviceSchema],
objects: [objectSchema],
},
});
mongoose.model('Company', companySchema);
mongoose.model('Object', objectSchema);
【问题讨论】:
标签: angularjs node.js mongodb mongoose