Передача переменной с помощью шаблонов EJS
Я использую шаблонизатор Ejs для своего проекта expressjs и, несмотря на передачу моих объектов в мой файл view blog.ejs, я получаюblogpost not defined
ошибка в моем файле ejs. Ошибка происходит у меня<% blogpost.forEach(function(blogpost) { %>
линия. Я полагаю, что это как-то связано с тем, как я передаю объект и его свойства, но я следовал инструкциям, и это кажется правильным.
routes.js:
//blog
router.route('/blog')
// START POST method
.post(function(req, res) {
var blogpost = new Blogpost(); // create a new instance of a Blogpost model
blogpost.title = req.body.title; // set the blog title
blogpost.author = req.body.author; // set the author name
blogpost.content = req.body.content; // set the blog content
blogpost.date = req.body.date; // set the date of the post
//Save Blog Post
blogpost.save(function(err) {
if (err)
res.send(err);
res.json({ message: 'Blog created.' });
});
}) // END POST method
// START GET method
.get(function(req, res) {
Blogpost.find(function(err, blogpost) {
if (err)
res.send(err);
blogpost.title = req.body.title; // update the blog title
blogpost.author = req.body.author; // set the author name
blogpost.content = req.body.content; // update the blog content
blogpost.date = req.body.date; // set the date of the post
res.render('pages/blog', {
title: blogpost.title,
author: blogpost.author,
content: blogpost.content,
date: blogpost.date
});
});
}); // END GET method
blog.ejs:
<html>
<head>
<% include ../partials/head %>
</head>
<body>
<header>
<% include ../partials/header %>
</header>
<div class="grid">
<div class="col-1-1">
<div class="body-content">
<% blogpost.forEach(function(blogpost) { %>
<h1><%= blogpost.title %></h1>
<% }); %>
</div>
</div>
</div>
<footer>
<% include ../partials/footer %>
</footer>
</body>
</html>