javascript - 填充条件失败

标签 javascript node.js sails.js waterline

使用以下函数,操作无法正确构建标准。

   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);
            }
        })
    }

它似乎忽略了用户模型的标准。 我该如何修复它?

最佳答案

填充方法有 2 个参数:

  1. 外键:属性名称
  2. 查询(可选):限制填充值列表

如示例 in the docs 所示,第二个参数不是必需的,仅当您具有一对多关联时才有用。

例如,如果您有以下模型:

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

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

以下是使用填充的一些方法:

// 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 }
*/

就您而言,您想要获取用户的文章。具体方法如下:

// 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) } ]
*/

关于javascript - 填充条件失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31138771/

相关文章:

node.js - 无法 GET 到 Node js 和 Express.js 中的路由

javascript - 在不修改原件的情况下将一个数组切片/拼接成另一个数组

javascript - 如何在 Javascript 中调用这个函数?

javascript - 如何给prettyphoto添加动画或效果

node.js - 在 Windows 上安装爆米花机开发人员

node.js - AppEngine Standard 和 NodeJS 的本地开发服务器

javascript - 如何将 json 响应中的二进制图像保存到 S3 存储桶

javascript - 如何在 sails.js 模型中使用数组属性

javascript - SailsJS 填充通过套接字publishUpdate发送的属性

mysql - 如何为现有的 sails 项目设置 sequelize