填充另一个属性时的空集合数组

时间:2019-04-10 19:54:51

标签: sails.js

Sails v0.12.14

模型Category

module.exports = {
  attributes: {
    name: {
      type: 'string'
    },
    brands: {
      collection: 'Brand',
      via: 'categories'
    },
    test: {
      model: 'Brand'
    }
  }
}

在这种情况下

Category.findOne(1)

将预期结果返回为

{
  id: 1,
  name: 'category_name',
  brand: 1
}

但是

Category.findOne(1).populate('test')

向响应添加空的brands数组:

{
  brands: [],
  id: 1,
  name: 'category_name',
  test: {brand_object}
}

Category.findOne(1).populate('test').populate('brands')

仍然可以正常工作。

0 个答案:

没有答案