Sails.js查询关联的值

我正在使用Sails.js版本0.10.0-rc4 。 所有的模型都使用sails-mysql。

我试图查询一个模型,有一个“一对多”关联到另一个模型(查询发生在“多”一侧)。

它看起来像这样:

 Post.find() .where({ category: category_id }) .populate("category") .exec( ... ) 

这给了我一个空数组,但是当我离开.populate("category")我得到正确的结果集。

我知道我可以离开.populate("category") ,然后分别获取每个相关的类别对象,但我想知道是否有更好的解决scheme,这个问题。

这不是100%清楚您要在这里做什么。 如果您的目标是过滤Post上填写的类别,您可以执行以下操作:

 Post.find() .populate("category", {where: { category: category_id }}) .exec( ... ) 

如果你只想检索某个类别的Post ,你可以这样做:

 Category.findOne(category_id) .populate("posts") .exec(function(e, c) {console.log(c.posts);}) 

原帖太早了

以下为我的作品(风帆0.12)与人口:

 Post.find({category: category_id}) .populate("category") .exec( ... ) 

但是,我仍然不知道是谁在除了id以外的其他属性中search子查询。

编辑2:似乎深查询尚不支持: https : //github.com/balderdashy/waterline/issues/266