[英]I got the error while uploading image in mongodb Cannot read property 'buffer' of undefined
When I upload the pdf using multer in mongodb in node.js then I got error "Cannot read property 'buffer' of undefined".I am trying to upload the pdf file in database.Please help to resolve this issue.Thanks in advance.当我在 node.js 中使用 mongodb 中的 multer 上传 pdf 时出现错误“无法读取未定义的属性‘缓冲区’”。我正在尝试上传数据库中的 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;
req.file
does not exist, that's why you got this error req.file
不存在,这就是你得到这个错误的原因
I think it should be req.files.file.buffer
我觉得应该是
req.files.file.buffer
if not, try to console.log(req)
, and trace it to find the file
如果不是,请尝试
console.log(req)
,并跟踪它以找到该file
if there is no files
or file
in the req
object, then the file
hasn't been sent to the request
如果
req
object 中没有files
或file
,则file
尚未发送到request
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.