Req.body 为空,也无法读取 属性 未定义的路径

Req.body is empty and also cannot read property path of undefined

我的项目直到上周都运行良好,现在突然间我的 post 请求不起作用了。我尝试了所有方法并阅读了堆栈溢出的其他问题,但无法解决问题。有人可以帮我吗?

问题:req.body 未定义,每当我尝试上传文件时,都会出现“无法读取 属性 未定义路径”的错误。

我正在使用 express 中间件来解析请求正文。我也有我的表格 enctype 到 multipart/form-data..

代码片段如下:

require('dotenv').config()
const express = require('express');
const app = express();
const router = express.Router();
const session = require('express-session');
const fs = require(`fs`);
const mysql = require(`mysql-await`);
const path = require('path');


 app.set('view engine', 'ejs');
 app.set('views', path.join(__dirname, 'views'));
 app.use(express.static(path.join(__dirname, 'public')));
 app.use(express.json())
 app.use(express.urlencoded({ extended: true }))

 const multer = require('multer');
 const {storage} = require('../cloudinary');
 const upload = multer({storage});

 const con = mysql.createConnection({
      host: "localhost",
      user: "root",
      password: "Sujanya@1978",
      database: "dept"
 });

 con.connect((err) => {
    if (!err) {
    console.log("Connected");
    }
    else {
    console.log(err)
    }
 })

router.get('/naaccircular',(req,res)=>{
    (async () => {
           let results = await con.awaitQuery('select* from dept.naaccircular;');
       res.render('Naac_circular',{Egs : results})
    })();

   })

  router.get('/naaccriteria',(req,res)=>{
   (async () => {
          flet results = await con.awaitQuery('select* from dept.naaccriteria;');
        res.render('Naac_criteria_files',{Fgs : results})
    })();
    })





    router.post('/naacaddcircular',upload.single('circularfile'),(req,res) => {
          console.log(req.body);
          const n = req.body.circularname;
          const d = req.body.circulardate;
          const l = req.file.path;


          con.connect(function(err){
          var records = [n,d,l];
          con.query("insert into dept.naaccircular (cirname,cirlink,cirdate) 
                  VALUES (?,?,?)", [n,l,d] , function (err, result, fields){
          if (err) throw err;
        
          })
    
    
          });
          console.log(n);
          console.log(l);
          console.log(d);
          res.redirect('/naaccircular');
      })


 module.exports = router;

    
        style="margin-top:80px; background-color: white;">
        <form action="/naacaddcircular" method="POST" class="row g-3 form-container" enctype="multipart/form-data">
        <h3 style="text-align: center;">Naac Circular</h3>
        <div class="mb-3">
            <label for="ii" class="form-label">Name</label>
            <input id="ii" name="circularname" class="form-control" type="text" placeholder="Default input"
                aria-label="default input example">
        </div>
        <div class="mb-3">
            <label for="jj" class="form-label">Date</label>
            <input id="jj" name="circulardate" class="form-control" type="date">
        </div>
        <div class="input-group mb-2">
            <input type="file" class="form-control" name="circularfile"id="inputGroupFile04" aria-describedby="inputGroupFileAddon04"
                aria-label="Upload">
            <!--<button class="btn btn-outline-secondary" type="button" id="inputGroupFileAddon04">Button</button>-->
        </div>
        <button type="submit"
            class="btn btn-primary position-relative start-50 botttom-0 translate-middle-x">Upload</button>
        </form>
    </div> ```
 

问题出在 cloudinary 模块上。我已经改正了,post方法没有错误。

一旦从 router.post() 函数中删除 upload.single() 函数,我就可以得到 req.body(),