最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Sails.js query on an associated value - Stack Overflow

programmeradmin2浏览0评论

I'm using Sails.js version 0.10.0-rc4. All models are using sails-mysql.

I'm trying to query a model which has an "one-to-many" association to another model (the query is happening on the "many" side).

It looks something like this:

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

This gives me an empty array back however when I leave out the .populate("category") I get the correct result set.

I know that I could leave .populate("category") out and then fetch each correlating Category object separately, but I'm wondering if there's a better solution to this problem.

I'm using Sails.js version 0.10.0-rc4. All models are using sails-mysql.

I'm trying to query a model which has an "one-to-many" association to another model (the query is happening on the "many" side).

It looks something like this:

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

This gives me an empty array back however when I leave out the .populate("category") I get the correct result set.

I know that I could leave .populate("category") out and then fetch each correlating Category object separately, but I'm wondering if there's a better solution to this problem.

Share Improve this question edited Mar 22, 2016 at 11:09 Johan Dettmar asked Apr 8, 2014 at 14:50 Johan DettmarJohan Dettmar 29.5k5 gold badges34 silver badges30 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 12

It's not 100% clear what you're trying to do here. If your goal is to filter the categories that get populated on the Post, you can do:

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

If you want to only retrieve Posts with a certain category, you would do:

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

Original post was too soon

Following works for me (sails 0.12) with population:

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

But I am still puzzled who to search in a subquery any other attribute than id..

Edit2: Seems like deep query is not yet supported: https://github./balderdashy/waterline/issues/266

发布评论

评论列表(0)

  1. 暂无评论