Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
497 views
in Technique[技术] by (71.8m points)

node.js - Passing Variable with EJS Templating

I am using the Ejs templating engine for my expressjs project and despite passing my objects along to my view blog.ejs file, I am receiving an blogpost not defined error in my ejs file. Error is happening at my <% blogpost.forEach(function(blogpost) { %> line. I figure that is has something to do with how im passing the object and its properties, but I followed guides and it appears correct.

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You're not passing an array variable called blogpost to your template, you are instead passing these variables to your template:

title: blogpost.title,
author: blogpost.author,
content: blogpost.content,
date: blogpost.date

You could just do this render() instead of the one you currently have:

res.render('pages/blog', {
  blogpost: blogpost,
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...