我正在尝试为我的记录创建一个标记系统,并假定我将能够通过在post方法上使用req.body.tags.trim().split(",");
来创建单独的标记,但是我提供的所有用逗号分隔的值都会显示为一个URL。
所写的值:
显示和悬停的值:
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
});
});
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>
最佳答案
是的,您正在将它们全部写入ejs模板的一个锚中。
像这样:
<ul>
<% for(var i=0; i<blog.tags.length; i++) { %>
<li>
<a href='/tags/<%= blog.tags[i] %>'>
<%= blog.tags[i] %>
</a>
</li>
<% } %>
</ul>
我假设您的保存逻辑已按照您的想法成功保存了数组,但这也值得检查
关于javascript - ExpressJS逗号分隔值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28112161/