javascript - Sails JS - 相同模型和属性的深度填充

标签 javascript node.js model sails.js populate

让我稍微解释一下。这个想法是有类别,每个类别也可以有一个父类别和一个子类别,但我们不知道这棵树可以有多深。例如:

->Clothes
-->Men
--->Kids
---->Newborns
----->Etc, etc
-->Women
-->Unisex

所以我认为我的 Category.js 模型可以具有以下属性:

module.exports = {
  attributes: {
    name: {
      type: 'string',
      required: true,
      unique: true
    },
    products: {
      collection: 'product',
      via: 'category'
    },
    parentCategory: {
      model: 'category'
    },
    subCategories: {
      collection: 'category',
      via: 'parentCategory'
    }
  }
};

当我获得所有类别时:

Category.find({}).populate('subCategories').exec(........

我得到了所有类别及其子类别的列表,但我也想拥有子类别的子类别,就像我之前编写的树层次结构一样,但我得到的是

衣服 { 子类别:[男士:{},女士:{},男女皆宜:{}] } 男人{......

但是在衣服里面,我有男人,这是正确的。但现在在这个 Men 对象中我没有“Kids”。

我说清楚了吗?

最佳答案

Populate 方法仅适用于一个级别(目前),因此您无法先填充对象,然后再填充其中的其他级别。我有一个类似的功能,我发现并编写了这段代码,也许它可以帮助你:

  return Object.findOne({
        id: id
      }).populateAll().then(function (result) {
         var otherObject= otherObject.find(result.id).then(function (otherResult) {
           return otherResult;
         });
         return [result, otherResult];
      }).spread(function (house, otherResult) {
         result= result.toObject();
         result.otherResult= otherResult;
         return result;
      });

关于javascript - Sails JS - 相同模型和属性的深度填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38255084/

相关文章:

ruby-on-rails - Rails - 在没有 url helper 的情况下在模型的虚拟属性中构建绝对 url

javascript - 为 d3 中的旭日加载内部 JSON 数据而不是从外部资源/文件

javascript - 如何检查字符串是否有超过 1 个整数(以逗号分隔)

php - laravel 我需要每个表的模型和 Controller 吗?

javascript - bookshelf.js 中的多表关系

javascript - 类型错误 : Callback is not a function with asyncJS in nodeJS

ruby-on-rails - Ruby On Rails 模型中的自保存

javascript - Grails jQuery-ui 自动完成

javascript - angular-tree-control 删除整体填充

node.js - SocketIO 在自签名证书上抛出 net::ERR_CERT_AUTHORITY_INVALID