首页 > 解决方案 > 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,因为当我尝试处理请求时它显示为未定义。我将如何解决这个问题?

谢谢

标签: javascriptnode.jsmongodbexpress

解决方案


推荐阅读