我正在尝试使用 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 } });
代码仍未过滤。我希望这个案子能够得到解决。谢谢:)
最佳答案
当您填充对象的关联时,名称必须是父对象中关联的名称,而不是您引用的模型的名称。
在您的情况下,社区有名为“城市”的关联。
下一步是过滤城市列表,这是作为 populate 的第二个参数的 where 语句。
Community.find()
.populate('cities', {
where: {
province: 1
}
}
)
关于javascript - Sails Js 填充条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60858880/