Pisix
Pisix

Reputation: 205

populate with criteria failed

Using the below function, the operations don't build the criteria correctly.

   getArticlesByUser:function(cb,val)
    {
        Article.find({state:'Normal'}).populate('user', {id: cb.iduser }).populate('images').populate('devise').exec(function(err,article){

            if(article)
            {
                    val(null,article);
            }
            if(err)
            {
                val(err,null);
            }
        })
    }

It seems to ignore the criteria on user model. How can I fix it ?

Upvotes: 2

Views: 59

Answers (1)

Yann Bertrand
Yann Bertrand

Reputation: 3114

The populate method takes 2 parameters:

  1. Foreign Key: the name of the attribute
  2. Query (optional): to limit the list of populated values

As seen in the examples in the docs, the 2nd argument is not required and only useful if you have a one-to-many association.

For example, if you have these models:

// Article.js
module.exports = {
  attributes: {
    author: { model: 'user' },
    title: { type: 'string' }
  }
};

// User.js
module.exports = {
  attributes: {
    articles: {
      collection: 'article',
      via: 'author'
    },
    name: { type: 'string' }
  }
};

Here are some ways to use populate:

// Get a user from his name
User.findOne({ name: 'Pisix' }).function(err, result) {
  console.log(result.toJSON());
});

/* 
{ name: 'Pisix',
  createdAt: Wed Jun 20 2015 14:07:25 GMT-0600 (CST),
  updatedAt: Wed Jun 20 2015 14:07:25 GMT-0600 (CST),
  id: 7 }
*/

// Get a user from his name + his articles
User.findOne({ name: 'Pisix' }).populate('articles').function(err, result) {
  console.log(result.toJSON());
});

/* 
{ articles:
    [ { title: 'The first article',
        id: 1,
        createdAt: Wed Jun 20 2015 14:12:33 GMT-0600 (CST)
        updatedAt: Wed Jun 20 2015 14:12:33 GMT-0600 (CST) },
      { title: 'Another article',
        id: 2,
        createdAt: Wed Jun 21 2015 15:04:12 GMT-0600 (CST)
        updatedAt: Wed Jun 21 2015 15:04:12 GMT-0600 (CST) },
      { title: 'I\'m back',
        id: 10,
        createdAt: Wed Jun 30 2015 13:25:45 GMT-0600 (CST)
        updatedAt: Wed Jun 30 2015 13:25:45 GMT-0600 (CST) } ],
  name: 'Pisix',
  createdAt: Wed Jun 20 2015 14:07:25 GMT-0600 (CST),
  updatedAt: Wed Jun 20 2015 14:07:25 GMT-0600 (CST),
  id: 7 }
*/

// Get a user from his name + one of his articles from its title
User.findOne({ name: 'Pisix' }).populate('articles' , { title: 'The first article' }).function(err, result) {
  console.log(result.toJSON());
});

/* 
{ articles:
    [ { title: 'The first article',
        id: 1,
        createdAt: Wed Jun 20 2015 14:12:33 GMT-0600 (CST)
        updatedAt: Wed Jun 20 2015 14:12:33 GMT-0600 (CST) } ],
  name: 'Pisix',
  createdAt: Wed Jun 20 2015 14:07:25 GMT-0600 (CST),
  updatedAt: Wed Jun 20 2015 14:07:25 GMT-0600 (CST),
  id: 7 }
*/

In your case, you want to get the articles of a user. Here is how to that:

// Get articles of a user from his id
Article.find({ author: 7 }).exec(function (err, result) {
  console.log(result.toJSON());
});

/* 
[ { title: 'The first article',
    id: 1,
    createdAt: Wed Jun 20 2015 14:12:33 GMT-0600 (CST)
    updatedAt: Wed Jun 20 2015 14:12:33 GMT-0600 (CST) },
  { title: 'Another article',
    id: 2,
    createdAt: Wed Jun 21 2015 15:04:12 GMT-0600 (CST)
    updatedAt: Wed Jun 21 2015 15:04:12 GMT-0600 (CST) },
  { title: 'I\'m back',
    id: 10,
    createdAt: Wed Jun 30 2015 13:25:45 GMT-0600 (CST)
    updatedAt: Wed Jun 30 2015 13:25:45 GMT-0600 (CST) } ]
*/

Upvotes: 2

Related Questions