首页 文章

在蓝图选项为ON的查找路径上填充关联字段

提问于
浏览
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"
  }
]

所以没有 parentschildren 属性的痕迹 .

确实在数据库中创建了关联,当我请求_1213150时,我得到了这个:

[
  {
    "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

1 回答

  • 1

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

    sails.config.blueprints.populate = false;
    

    opened an issue知道它为什么在全球范围内完成以及如何解决问题 .

相关问题