Node.js 按嵌套ID查找文档
试图通过嵌套idNode.js 按嵌套ID查找文档,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,试图通过嵌套id(author.id)识别正确语法以检索作者的文章。抛出SyntaxError,找不到解决方案 我的路线: router.get('/articles/authors/:id', function(req, res){ Article.find( {author.id: req.params.id} ).sort( {createdAt: -1} ).exec(function(err, articles){ if(err){
(author.id)
识别正确语法以检索作者的文章。抛出SyntaxError,找不到解决方案
我的路线:
router.get('/articles/authors/:id', function(req, res){
Article.find( {author.id: req.params.id} ).sort( {createdAt: -1} ).exec(function(err, articles){
if(err){
console.log(err);
return req.flash('error', 'Unable to find author.');
} else {
console.log(articles);
res.render('authors/', {
articles: articles,
pagetitle: 'Articles by '
});
}
});
});
我的articleSchema的作者部分如下:
author:
{
id:
{
type: mongoose.Schema.Types.ObjectId,
ref: "User"
},
username: String,
name: String
},
如果有人能看到我的错误,我将不胜感激。用引号括起嵌套路径。看哪个说: 要指定嵌入/嵌套文档中字段的查询条件,请使用点表示法(“field.nestedField”) 所以你的代码
Article.find( {author.id: req.params.id} )
应替换为
Article.find( {"author.id": req.params.id} )
明亮的当我试图弄明白这一点时,我在这两个词周围都加了引号,但这并没有起作用。非常感谢Benjamin花时间回复。也感谢上面的报价。谢谢你的链接,因为我是在搜索猫鼬而不是Mongodb。没问题,很高兴我能帮上忙!