Node.js 如何在节点中访问数组中的对象
我正在尝试访问博客架构内的正文。我该怎么做呢 模式: ** 路由器 ** ** publishedArticle.ejs **Node.js 如何在节点中访问数组中的对象,node.js,schema,ejs,Node.js,Schema,Ejs,我正在尝试访问博客架构内的正文。我该怎么做呢 模式: ** 路由器 ** ** publishedArticle.ejs ** 我没有定义您已将您的博客架构声明为对象数组(请注意,您已将对象周围的[]用于博客项目)。如果这是有意的,那么您需要使用数组索引访问文章中的各种博客元素(或者使用循环对它们进行迭代)。下面的代码段假定您的文章至少保存了一个博客条目: <h3><%= article.blog[0].body %></h3> 谢谢,看起来很简单 ro
我没有定义您已将您的博客架构声明为对象数组(请注意,您已将对象周围的[]用于博客项目)。如果这是有意的,那么您需要使用数组索引访问文章中的各种博客元素(或者使用循环对它们进行迭代)。下面的代码段假定您的文章至少保存了一个博客条目:
<h3><%= article.blog[0].body %></h3>
谢谢,看起来很简单
router.get('/blog/article/:postid', function (req, res, next) {
Article.findById({ _id: req.params.postid }, function (err, article) {
if (err) return next(err);
res.render('main/publishedArticle', {
article: article,
message: req.flash('showing article ' + article.title)
});
});
});
<h3><%= article.blog.body %></h3>
<h3><%= article.blog[0].body %></h3>