【问题标题】:I got the error while uploading image in mongodb Cannot read property 'buffer' of undefined在 mongodb 中上传图像时出现错误无法读取未定义的属性“缓冲区”
【发布时间】:2020-04-14 21:46:18
【问题描述】:

当我在节点 js 中使用 mongodb 中的 multer 上传 pdf 时,出现错误“无法读取未定义的属性 'buffer'”。我正在尝试将 pdf 文件上传到数据库中。请帮助解决这个问题。谢谢前进。

const express=require('express');
const multer=require('multer');
const getstream = require('get-stream');
const resume=require('../model/Resume')

const router = new express.Router();
const uplaod = multer({

    limits:{
      fileSize: 100000
    },

    fileFilter(req,file,cb)
    {
      if(!file.originalname.endsWith('pdf'))
      {
        return cb(new Error('Upload the pdf file'))
      }

      cb(undefined,true);
    }
  });
router.post('/Resume',uplaod.single('avatar'),async (req,res)=>{


     req.resume.avatar=req.file.buffer
      await req.resume.save();
      res.send({sucess:'sucess'});
    },(error,req,res,next)=>{

      //It is used for print message otherwise it will give long html message

      res.status(404).send({error:error.message});
    })



  module.exports=router;

【问题讨论】:

    标签: node.js mongodb express mongoose


    【解决方案1】:

    req.file 不存在,这就是您收到此错误的原因

    我觉得应该是req.files.file.buffer

    如果没有,请尝试console.log(req),并追踪它以找到file

    如果req对象中没有filesfile,则file还没有发送到request

    【讨论】:

      猜你喜欢
      • 2021-06-13
      • 1970-01-01
      • 2021-07-30
      • 2019-01-20
      • 1970-01-01
      • 2021-12-10
      • 1970-01-01
      • 2019-11-14
      • 2017-06-01
      相关资源
      最近更新 更多