3

我的应用程序有一个“类别”模型。

类别可以是其他类别的子类别。

所以有一个“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"
  }
]

所以没有parentsandchildren属性的踪迹。

当我要求/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蓝图的配置键定义:

蓝图控制器是否应该使用来自通过关联链接的其他模型的数据填充模型获取。如果您在一对多关联中有大量数据,则将其打开可能会导致非常繁重的 api 调用。

该值true在我的项目中,但仍然没有填充关联属性。

我误解了文档还是我的项目有问题?

我使用帆 0.11.x

4

1 回答 1

1

问题是我正在使用sails-permissions,它覆盖了蓝图的populate配置:

sails.config.blueprints.populate = false;

打开了一个问题,以了解为什么它在全球范围内完成以及如何解决问题。

于 2015-08-25T13:01:07.897 回答