【问题标题】:Sails Js populate with conditionsSails Js 填充条件
【发布时间】:2020-03-25 23:27:06
【问题描述】:

我正在尝试使用sails-hook-deep-orm 在sails js 中进行多次填充。我想过滤省份ID。 我正在使用最新版本的 Sails 和 Node。我使用 MySQL 作为数据库。

这是我的模型:

// Province.js
attributes: {
    name: {
      type: 'string',
      required: true
    },
    description: {
      type: 'string',
      required: false
    },
    cities: {
      collection: 'city',
      via: 'province'
    }
}

// City.js
attributes: {
    name: {
      type: 'string',
      required: true
    },
    description: {
      type: 'string',
      required: false
    },
    province: {
      model: 'province'
    },
    communities: {
      collection: 'community',
      via: 'city'
    }
}

// Community.js
attributes: {
    name: {
      type: 'string',
      required: true
    },
    description: {
      type: 'string',
      required: false
    },
    cities: {
      collection: 'city',
      via: 'province'
    }
}

我已经尝试过:

Community.find()
  .populate('city.province', {'city.province.id' : 1});

Community.find()
  .populate('city.province', { where: { 'city.province.id': 1 } });

代码仍未过滤。我希望这个案子能得到解决。谢谢你:)

【问题讨论】:

    标签: javascript node.js sails.js sails-orientdb


    【解决方案1】:

    当您填充对象的关联时,名称必须是父对象中关联的名称,而不是您所引用的模型的名称。

    在您的情况下,Community 有一个名为“城市”的关联。

    下一步是过滤城市列表,这是 where 语句作为填充的第二个参数。

    Community.find()
     .populate('cities', { 
        where: { 
          province: 1 
        }
      }
    )
    

    【讨论】:

    • 虽然此代码可以解决问题,including an explanation 说明如何以及为什么解决问题将真正有助于提高您的帖子质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人。请edit您的答案以添加解释并说明适用的限制和假设。 From Review
    • 对不起,我在 community.js 属性中给出了错误的模型:{ name: { type: 'string', required: true }, description: { type: 'string', required: false }, city: { model: 'city' }, },这个是真模特,这是一对一的,谢谢:)
    猜你喜欢
    • 1970-01-01
    • 2017-02-07
    • 2015-04-03
    • 2015-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多