Express Req.Body基于回调列表中的中装位置为空

发布于 2025-02-03 02:49:49 字数 1353 浏览 3 评论 0原文

抬头,我对节点相对较新,表达这可能是一个简单的答案,但我不明白为什么一个中装有效,而另一个不起作用。

//Function i'm trying to use as middle-wear 
const setRecordID = function (req, res, next) {
  console.log(req.body)
  console.log("user id: " + req.user.id.toString())
  console.log("number plate: " + req.body.numberPlate)
  console.log("dateTime: " + Date.now().toString() + "\n\n")
  return next();
}

我在以下函数中调用它,

app.post('/record/upload', setRecordID, upload.array('images', 12), setRecordID ,async function(req, res) {

      //this returns the number plate - works
      //console.log(req.body.numberPlate);

  
      // Steps 
      // removed code to shorten question
      // 4) redirect to home after adding new record   
      res.redirect('/home');
    });

因为您可以看到之前有一个setRecorDID函数调用,setRecordId在upload.Array('images',12)之后。上载函数是用于上传图像的Multer中装。如果需要它的代码,我可以将其发布,并且req.body在multer.filename函数中也具有值。

这是输出从'/record/upload'调用

“

显然一个作品,另一个作品无效。我正在使用express.urlencoded({extendend:true}),然后我也尝试添加身体偏好器(ik我不需要,而只是incase)。我还尝试在中装地点直接编写功能(RES,REQ,NEXT),而行为是相同的。上载之前的函数array返回一个空{} req.body和upload.array返回{numberplate:'12341'}。

Heads up, i'm relatively new to node, express so it might be a simple answer but i don't understand why one middle-wear works and the other doesn't.

//Function i'm trying to use as middle-wear 
const setRecordID = function (req, res, next) {
  console.log(req.body)
  console.log("user id: " + req.user.id.toString())
  console.log("number plate: " + req.body.numberPlate)
  console.log("dateTime: " + Date.now().toString() + "\n\n")
  return next();
}

Im calling it in the following function

app.post('/record/upload', setRecordID, upload.array('images', 12), setRecordID ,async function(req, res) {

      //this returns the number plate - works
      //console.log(req.body.numberPlate);

  
      // Steps 
      // removed code to shorten question
      // 4) redirect to home after adding new record   
      res.redirect('/home');
    });

As you can see there is a setRecordId function call before and a setRecordId after upload.array('images', 12). The upload.array function is multer middle-wear used to upload images. If its code is required i can post it, and the req.body has value in the multer.filename function as well.

Here's the output from the '/record/upload' call

output

Clearly one works and the other doesn't. I'm using express.urlencoded({extended: true}), and i tried adding body-parser too (ik i didn't need to but just incase). I also tried just directly writing the function(res,req,next) in the middle-wears place and the behavior is the same. The function before upload.array returns an empty {} req.body and after the upload.array returns { numberPlate: '12341' }.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文