Express 使用“提交数据时,请求正文为空”;表格数据“;

Express 使用“提交数据时,请求正文为空”;表格数据“;,express,postman,Express,Postman,当我使用原始JSON更新时,它工作正常,但当我使用表单数据时,它不会更新。使用表单数据时,请求主体是空对象。为什么会这样 这是我的更新代码: exports.updateProgram = catchAsync(async (req, res, next) => { console.log('req ko body',req.body) let doc = await Program.findByIdAndUpdate(req.params.id, req.body, {

当我使用原始JSON更新时,它工作正常,但当我使用表单数据时,它不会更新。使用表单数据时,请求主体是空对象。为什么会这样

这是我的更新代码:

exports.updateProgram = catchAsync(async (req, res, next) => {
    console.log('req ko body',req.body)
    let doc = await Program.findByIdAndUpdate(req.params.id, req.body, { runValidators: true, new: true })
    if (!doc) {
        return next(new AppError('No document found with that ID', 404))
    }
    res.status(200).json({
        status: 'success!',
        data: { doc }
    })
})
邮递员:

我正在使用multer,我实际上在req.body中传递了照片。代码如下:

let multerStorage = multer.memoryStorage()

let multerFilter = (req, file, cb) => {
    if (file.mimetype.split('/')[0] == 'image') {
        cb(null, true)
    } else {
        cb(new AppError('Not an image!', 400), false)
    }
}

let upload = multer({
    storage: multerStorage,
    fileFilter: multerFilter
})

exports.uploadPhotos =  upload.fields([
    { name: 'abcd', maxCount: 10 },
    { name: 'photos', maxCount: 10 },
    {name: 'photos3', maxCount: 10}
])

exports.resizePhotos = catchAsync(async (req, res, next) => {
    // if (!req.files.photos || !req.files.abcd) return next()
    if(req.files.abcd) {
    req.body.abcd = []
   await Promise.all(req.files.abcd.map(async (file, i) => {
       let filename = `tour-${Date.now()}-${i + 1}.jpeg`
       await sharp(file.buffer)
       .resize(500,500)
       .toFormat('jpeg')
       .jpeg({ quality: 90 })
       .toFile(`public/img/arpit/${filename}`)
       req.body.abcd.push(filename)
   }) 
   )} else if(req.files.photos3) {
    req.body.photos3 = []
    await Promise.all(req.files.photos3.map(async (file, i) => {
        let filename = `tour-${Date.now()}-${i + 1}.jpeg`
        await sharp(file.buffer)
        .resize(500,500)
        .toFormat('jpeg')
        .jpeg({ quality: 90 })
        .toFile(`public/img/arpit/${filename}`)
        req.body.photos3.push(filename)
    }) 
    )}
   else if(req.files.photos) {
    // console.log('codee here')
    // } else if(req.body.photos) {
     req.body.photos = []
     console.log('req.files>>>', req.files)
    await Promise.all(req.files.photos.map(async (file, i) => {
        let filename = `tour-${Date.now()}-${i + 1}.jpeg`
        await sharp(file.buffer)
        .resize(500,500)
        .toFormat('jpeg')
        .jpeg({ quality: 90 })
        .toFile(`public/img/programs/${filename}`)
        req.body.photos.push(filename)
    })
    )
}
    return next()
})
我正在导入路由文件

Express(bodyParser)无法处理多部分表单数据,这就是代码无法工作的原因

看一看,一个快递包裹。它是一个中间件,提供您需要的功能。

Express(bodyParser)无法处理多部分表单数据,这就是代码无法工作的原因

var cpUpload = upload.fields([{ name: 'avatar', maxCount: 1 }, { name: 'gallery', maxCount: 8 }]);
app.post('/cool-profile', cpUpload, function (req, res, next) {


    // req.files is an object (String -> Array) where fieldname is the key, and the value is array of files
      //
      // e.g.
      //  req.files['avatar'][0] -> File
      //  req.files['gallery'] -> Array
      //
      // req.body will contain the text fields, if there were any

})
看一看,一个快递包裹。它是一个中间件,提供您想要的功能

var cpUpload = upload.fields([{ name: 'avatar', maxCount: 1 }, { name: 'gallery', maxCount: 8 }]);
app.post('/cool-profile', cpUpload, function (req, res, next) {


    // req.files is an object (String -> Array) where fieldname is the key, and the value is array of files
      //
      // e.g.
      //  req.files['avatar'][0] -> File
      //  req.files['gallery'] -> Array
      //
      // req.body will contain the text fields, if there were any

})
这可能对你有帮助。引自


这可能对你有帮助。引用自

您能告诉我们您是如何发送此表单数据的吗?@EnriqueBermúdez检查编辑后的邮件您能告诉我们您是如何发送此表单数据的吗?@EnriqueBermúdez检查编辑后的邮件您在哪里导入
更新程序
功能?我能看到这一部分吗?我看不到multer中间件在这两条路径上的使用情况。您在哪里导入
updateProgram
函数?我能看到这一部分吗?我看不到multer中间件在这两条路由上的使用情况。