ExpressJS逗号分隔值

时间:2015-01-23 14:35:12

标签: javascript node.js express ejs

我正在尝试为我的记录创建一个标记系统,并假设我可以在我的post方法上使用req.body.tags.trim().split(",");创建单独的标记,但我提供的所有逗号分隔值都被置换为一个网址。

正在编写的值: inputted values

显示和悬停的值: image hover

POST方法:

router.route('/admin/posts/create')

    // START POST method
        .post(function(req, res) {

            console.log("New instance");

            var blogpost = new Blogpost(); // create a new instance of a Blogpost model

            blogpost.title = req.body.title; // set the blog title
            blogpost.featureImage = req.body.featureImage; // set the blog image
            blogpost.blogUrl = blogpost.title.toLowerCase().replace(/\s+/g,"-");
            blogpost.author = req.body.author; // set the author name
            blogpost.tagline = req.body.tagline; // set the tagline
            blogpost.content = req.body.content; // set the blog content
            blogpost.category = req.body.category; // set the category
            blogpost.tags = req.body.tags.trim().split(","); // set the tags
                //Save Blog Post
                blogpost.save(function(err) {
                    if (err)
                        res.send(err);

                    res.redirect(303, '/'); //NEEDS TO BE CHANGED
                });

        }) // END POST method


        .get(isLoggedIn, function(req, res, blogpost) {
            res.render('pages/blogpost-create', {
              blogpost : blogpost
            });
        });

志文章-create.ejs:

 <form action="/admin/posts/create" method="POST">
                    Header Image: <input type="file" id="files"/>
                    <p id="status">Please select a file</p>
                    <div id="preview"><img src="/images/event-placeholder.png"></div>
                    <input type="hidden" id="feature-image" name="featureImage" value="/images/event-placeholder.png" />
                    Title: <input type="text" name="title" required></input><br>
                    Author: 
                        <select name="author">
                            <option value="Author">Blog Author</option>
                        </select><br>
                    Category: 
                        <select name="category">
                            <option value="Test 1">Test 1</option>
                            <option value="Test 2">Test 2</option>
                            <option value="Test 3">Test 3</option>
                            <option value="Test 4">Test 4</option>
                        </select><br>
                    Tagline: <input type="text" maxlength="155" name="tagline" required><br>
                    Content:<br>
                    <textarea name="content" id="blog-editor" rows="10" cols="80">

                    </textarea>
                    <br>
                    Tags: <input type="text" name="tags" required>
                    <br>
<input type="submit" value="Submit">

Blogpost.ejs(显示值的位置):

<header>
    <% include ../partials/header %>
</header>

<div class="container">
    <div class="col-md-12">
        <div id="blog-content">
            <h1><%= blogpost.title %></h1>
            <img src="<%= blogpost.featureImage %>" class="img-responsive" id="blogpost-feature-image">
            <h3 class="blog-date"><%= blogpost.dateString %></h3>
            <h3 class="blog-category"><%= blogpost.category %></h3>
            <h3 class="blog-tagline"><i><%= blogpost.tagline %></i></h3>
            <p><%- blogpost.content %></p>
            <a href="<%= blogpost.tags %>"><%= blogpost.tags %></a>
        </div>
    </div>
</div>

1 个答案:

答案 0 :(得分:0)

是的,你将它们全部写入你的ejs模板中的一个锚点。

这样的事情:

<ul>
    <% for(var i=0; i<blog.tags.length; i++) { %>
        <li>
            <a href='/tags/<%= blog.tags[i] %>'>
                <%= blog.tags[i] %>
            </a>
        </li>
    <% } %>
</ul>

我假设你的保存逻辑成功地按照你的想法保存了数组,但这也是值得检查的