【发布时间】:2017-08-17 03:01:33
【问题描述】:
我正在创建一个博客,您可以在博客中添加 cmets(显然)。在我的 mongodb 模式中,注释对象如下:
var commentSchema = mongoose.Schema({
id: mongoose.Schema.Types.ObjectId,
text: String,
created: {type: Date, default: Date.now},
author: {
id: {
type: mongoose.Schema.Types.ObjectId,
ref: "User"
},
username: String,
image: String
}
});
我正在提取时间戳(已创建)并在使用以下内容发布评论时显示它:
<div id="comments">
<% blog.comments.forEach(function(comment){ %>
<div class="jumbotron comment">
<div class="row">
<div class="col-md-1">
<img class="comment-ico" src = "<%=comment.author.image%>">
</div>
<div class="col-md-7">
<h4><%=comment.author.username%></h4>
</div>
<div class="col-md-4 date">
<%= comment.created.toDateString()%>
</div>
</div>
</div>
<div><p><%=comment.text%></p></div>
但是,这只是以以下格式显示日期:Fri Mar 24 2017
我想显示的是发表评论后的时间。例如:“1 min ago”、“10 mins ago”等。如何使用JS来显示?
同样,如果我想显示日期,如何重新格式化为 mm/dd/yyyy?
谢谢
更新:
这是我的 cmets 创建路由,存储在 routes/comment.js 中:
router.post("/", middleware.isLoggedIn, function(req, res){
// lookup blog using id
Blog.findById(req.params.id, function(err, blog){
if(err) {
console.log(err);
res.redirect("/blogs");
} else {
// create new comment
Comment.create(req.body.comment, function(err, comment){
if(err) {
req.flash("error", "Something went wrong");
console.log(err);
} else {
comment.author.id = req.user._id;
comment.author.username = req.user.username;
comment.author.image = req.user.image;
comment.save();
// connect new comment to campground
blog.comments.push(comment);
blog.save();
var commentCreated = comment.created.toDateString();
if(req.xhr){
res.json({comment: comment, commentCreated: commentCreated, blog: blog});
} else {
// // redirect to campground show page
req.flash("success", "Successfully added comment");
res.redirect("/blogs/" + blog._id);
}
}
});
}
});
});
然后我在一个单独的文件 (/public/ajax.js) 中使用 AJAX 来异步显示:
$('#newComment').submit(function(e){
e.preventDefault();
var formData = $(this).serialize();
var formAction = $(this).attr('action');
$.post(formAction, formData, function(data){
console.log(data);
$("#comments").append(
`<div class="jumbotron comment">
<div class="row">
<div class="col-md-1">
<img class="comment-ico" src = "${data.comment.author.image}">
</div>
<div class="col-md-7">
<h4>${data.comment.author.username}</h4>
</div>
<div class="col-md-4 date">
${data.commentCreated}
</div>
</div>
</div>
<div id="A<%=comment._id%>"><p>${data.comment.text}</p></div>
<form id="edit-comment-form" action = "/blogs/data._id %>/comments" method = "POST" id="newComment">
<textarea class = "form-control" rows="4" placeholder = "Type comment here..." name = "comment[text]"></textarea>
<button class = "btn btn-lg btn-primary btn-block">Submit</button>
</form>
<div class="row" id="B${data.comment._id}">
<div class="col-md-1 choice">
<a class="edit">Edit</a>
</div>
<div class="col-md-1 choice1">
<form id = "delete-form" action = "/blogs/${data.blog._id}/comments/${data.comment._id}?_method=DELETE" method = "POST">
<input type = "submit" class = "button-delete" value = "Delete">
</form>
</div>
</div>
<hr class = "style-three">`
);
$('#newComment').find('.form-control').val('');
});
});
【问题讨论】:
标签: javascript mongodb mongoose momentjs ejs