javascript - NODE.JS 错误:无法在视图目录中查找视图“/articles/[object Object]”
问题描述
大家好,我目前正在为我的网站创建评论系统。但是,我收到标题中的错误。我的表格
<form action="/comments" enctype="multipart/form-data" method="POST">
<input type="hidden" name="article_id" id="article_id" value="<%= article._id %>" />
<input type="hidden" name="user_id" id="user_id" value="<%= currentUser._id %>" />
<div class="form-group">
<textarea required name="content" id="content" class="form-control"></textarea>
</div>
<button type="submit" class="btn btn-primary">Post</button>
<script type="text/javascript">
textarea = document.querySelector("#post");
textarea.addEventListener('input', autoResize, false);
function autoResize() {
this.style.height = 'auto';
this.style.height = this.scrollHeight + 'px';
}
</script>
</form>
这是处理表单的代码
const express = require('express')
const router = express.Router()
const path = require('path')
const Article = require('./../models/article')
const Comment = require('./../models/comment')
const User = require('./../models/user')
router.post('/', async (req, res, next) => {
console.log(req.body.post)
req.comment = new Comment()
let comment = req.comment
let user = await User.findOne(req.body.user_id)
let article = await Article.findOne(req.body.article_id)
comment.content = req.body.content
comment.userName = req.user.name
user.comments.push(comment._id)
article.comments.push(comment._id)
try {
article = await article.save()
comment = await comment.save()
res.redirect(`/articles/${article.slug}`)
} catch (e) {
res.render(`/articles/${path}`, { article: article })
}
})
module.exports = router
我已经确定节点没有正确传输 req.body.content,因为当我尝试处理请求时它显示为未定义。我将如何解决这个问题?
谢谢
解决方案
推荐阅读
- arduino - 在 Arduino IR 接收器中接收信号一次
- azure - 处理 Azure Function 中的 HttpStatus 302
- angularjs - 如何设置组标签排序angularjs ui-select group-by
- python - 如何根据其他列(Python)将值放入 Dataframe 的列中?
- sql - 如果该月没有条目,如何选择该月 SQL 的最后一天
- python - 删除python中特定范围之外的日期
- android - java.lang.RuntimeException:无法反序列化对象。期望一个列表,但得到一个类 java.lang.String(在字段“图像”中找到)
- php - 如何将一些警报或消息发送到另一个 php 文件
- python-3.x - Python 类型注解减慢代码速度
- ios - 共享扩展代码未获取 YouTube 应用 URL