sails.js - 在蓝图选项开启的情况下,在查找路线上填充关联字段

标签 sails.js waterline model-associations sails-permissions

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

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

所以有一个“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 属性的痕迹。

当我请求 /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/

相关文章:

node.js - 为什么启动 sails 时会出现有关 Grunt 的错误

node.js - 在sails.js 中覆盖CRUD 蓝图

node.js - 多种型号的水线交易

node.js - Sequelize, 一对多关联, find with include

javascript - 在 ubuntu 服务器上手动安装 sails

node.js - 一对一关联是否应该填充两侧?

javascript - 为什么功能流程不正常?

php - CakePHP 对非对象成员函数 schema() 的 fatal error 调用

hibernate - 将数据库值重定向到另一个对象的字段

node.js - 使用 SailsJS,有没有办法根据记录中的某个值进行 Model.watch() ?