【问题标题】:req.body is [Object : null prototype]{} while using multerreq.body 是 [Object : null prototype]{} 使用 multer
【发布时间】:2021-11-16 21:46:21
【问题描述】:

我想用 multer 在 nodejs 中上传图片,但我有一些问题

在我的 server.js(主要)中,我有

const express = require('express');
const app = express();
const expressBusboy = require('express-busboy');
expressBusboy.extend(app,{upload : true , path : 'uploads/'});

在 routes/index.js 我有

const multer = require('multer')
const upload = multer()
router.post('/products',upload.single('photo'),handeler)
router.post('/test',handeler)

handeler func 只是在控制台中记录一些东西现在我有 3 个问题
1:当我向 /test 发送一个不包括 upload.single 的请求时,req.body 对象被定义并且它没问题,但是当我向 /products 发送相同的请求时,其中包括 upload.single('some name') req。文件已定义且正常,但 req.body 为 [Object : null prototype]{} 为什么会这样?
2:为什么当我使用 upload.single() 我有 req.files 而不是 req.file 而 req.files 是一个只有一个对象的对象
3:当我收到请求时,我要上传的文件将存储在一个名为 uuid 的新文件夹中,而不是直接保存在我已经创建的上传文件夹中,我尝试添加 allowedPath :/^/upload$/ (我复制了这个正则表达式在这里可能是错误的)但它停止存储
并且 iam 使用邮递员发送请求,iam 使用表单数据作为正文,并且在我检查文件之前添加了文本,并且请求内容类型的标题已设置为 multipart/form-data

【问题讨论】:

    标签: node.js express middleware multer busboy


    【解决方案1】:

    我发现了问题 发生这种情况是因为我同时使用了 busboy 和 multer 我开始发送表单数据而不上传文件,我需要 busboy 来解析请求正文,之后我安装了 multer 来上传我的图像,这使得它们相互覆盖,出现了问题

    【讨论】:

      猜你喜欢
      • 2021-03-10
      • 1970-01-01
      • 2020-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-28
      • 1970-01-01
      相关资源
      最近更新 更多