【发布时间】:2020-01-17 00:10:24
【问题描述】:
我正在使用 JavaScript、Express 和 ejs 模板制作博客。每篇博客文章都可以发表评论,我希望在每条评论中发布日期。 cmets 的内容是从 .ejs 文件的输入字段中提取的值。现在我正在使用 moment.js,但日期/时间不断更新。我希望它只显示创建评论的日期/时间。
这是我的 app.js 文件中的一些代码:
moment = require('moment');
app.locals.moment = moment
这是我创建评论的途径:
app.post("/comments/:id", (req, res) => {
Blog.findById(req.params.id, (err, blog) => {
if (err) {
console.log("Something went wrong:", err);
} else {
console.log("BLOG", blog);
let newComment = { comment: req.body.comment };
Comment.create(newComment.comment, (err, comment) => {
if (err) {
console.log("Something went wrong:", err);
} else {
console.log("Success! Comment Posted", comment);
comment.save();
blog.comments.push(comment);
blog.save();
res.redirect("/blog/" + blog._id);
}
});
}
});
});
从我的 .ejs 文件中:
<em><%= moment(Date()).format(shortDateFormat) %></em>
我认为这应该足以了解它的要点。这可能是一个非常容易解决的问题,但我仍然是一个非常绿色的编码员!
【问题讨论】:
标签: javascript mongodb express momentjs ejs