使用条件填充失败
populate with criteria failed
使用以下函数,操作无法正确构建标准。
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);
}
})
}
它似乎忽略了user
模型上的准则。我该怎么修?
populate方法采用两个参数:
- 外键:属性的名称
- 查询(可选):限制已填充值的列表
正如文档中的示例所示,第二个参数不是必需的,只有当您有一对多关联时才有用。
例如,如果您有以下型号:
// Article.js
module.exports = {
attributes: {
author: { model: 'user' },
title: { type: 'string' }
}
};
// User.js
module.exports = {
attributes: {
articles: {
collection: 'article',
via: 'author'
},
name: { type: 'string' }
}
};
以下是使用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 }
*/
在您的情况下,您希望获得用户的文章。以下是如何做到这一点:
// 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) } ]
*/
相关文章:
- 如何设置html元素填充的动画
- 如何使用jquery在填充自动完成的值后使文本框只读
- 如何通过ajax刷新JSF填充的javascript变量
- 用与线条相同的颜色填充多折线图上的点
- 用每小时的差值填充数组/列表-从下拉列表中给定两个时间值
- 当json解析空响应时,Whatwg-Fetch失败,我该如何防止它
- 带有填充属性的SVG矩形显示在包含元素的上方插入框阴影
- 用我的json数据填充JQuery DataTable
- 使用 jquery 填充选择选项失败
- 使用带图案的填充时,canvas.toblob将失败
- 用户使用javascript生成的输入字段,如果验证失败,我如何将他们的数据填充回输入字段
- Javascript/PHP在表单提交时重新填充下拉列表失败
- D3从csv填充数据集失败
- 通过AJAX填充下拉菜单失败
- 在Codeigniter验证失败后重新填充动态javascript
- 在IE7, 8和9中填充列表时脚本失败
- 当最后一个输入被填充时,JQuery ajax调用失败
- 使用条件填充失败
- Dynamic Directive元素在来自服务的预填充数据中失败
- 为什么在javascript中填充2D数组失败