我的应用有一个“类别”模型。
类别可以是其他类别的子类别。
所以有一个“CategoriesAssociations”模型。
代码如下:
/* api/models/Categories.js */
module.exports = {
attributes: {
name: {
type: "string"
},
parents: {
collection: "categoriesassociations",
via: "child"
},
children: {
collection: "categoriesassociations",
via: "parent"
}
}
}
/* api/models/CategoriesAssociations.js */
module.exports = {
attributes: {
parent: {
model: "categories"
},
child: {
model: "categories"
}
}
}
现在,当我使用 find
路由又名 /categories
时,我得到了这个:
[
{
"createdAt": "2015-08-24T14:16:46.662Z",
"updatedAt": "2015-08-24T14:24:23.819Z",
"name": null,
"id": "55db274e424996cc7e7512e2"
},
{
"createdAt": "2015-08-24T14:18:29.748Z",
"updatedAt": "2015-08-24T14:18:41.105Z",
"name": "test",
"id": "55db27b5424996cc7e7512e4"
}
]
所以没有 parents
和 children
属性的痕迹。
当我请求 /categories/55db27b5424996cc7e7512e4/children
时,确实在数据库中创建了关联我得到了这个:
[
{
"parent": "55db27b5424996cc7e7512e4",
"child": "55db274e424996cc7e7512e2",
"createdAt": "2015-08-24T14:32:43.429Z",
"updatedAt": "2015-08-24T14:32:43.429Z",
"id": "55db2b0bc97cc73083017f60"
}
]
Sails 文档声明蓝图的 populate
配置键定义:
Whether the blueprint controllers should populate model fetches with data from other models which are linked by associations. If you have a lot of data in one-to-many associations, leaving this on may result in very heavy api calls.
在我的项目中该值为 true
,但仍然没有填充关联属性。
是我误解了文档还是我的项目有问题?
我使用 sails 0.11.x
最佳答案
问题是我正在使用 sails-permissions它覆盖了蓝图的 populate
配置:
sails.config.blueprints.populate = false;
我opened an issue了解为什么它在全局范围内完成以及如何解决问题。
关于sails.js - 在蓝图选项开启的情况下,在查找路线上填充关联字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32186383/