Multer - 无法读取未定义的属性“缓冲区”

Posted

技术标签:

【中文标题】Multer - 无法读取未定义的属性“缓冲区”【英文标题】:Multer - Cannot read property 'buffer' of undefined 【发布时间】:2021-07-30 09:11:24 【问题描述】:

我在将图像文件上传到我的服务器时遇到问题,我在 YouTube 上观看了一些关于 multer 的教程,我做了与教程中完全相同的事情,无论出于何种原因,我收到一个错误:(“无法读取属性 'buffer' of undefined"),并且 req.file 也是未定义的。 我用谷歌搜索了这个错误,发现有些人有同样的问题,我试图像他们一样解决它,但它对我不起作用。

组件数据应用

newPostByUser(token, formData) 
    return (async () =>
      await call(`$this.url/lost`, 
        method: "POST",
        headers: 
          Authorization: `Bearer $token`,
          "Content-Type": "multipart/form-data",
        ,
        body: formData,
        timeout: this.timeout,
      ))();
  ,

组件图像解析

const path = require("path");
const multer = require("multer");
const storage = multer.memoryStorage();
const limits =  fileSize: 2000000 ;
const upload = multer(
  storage,
  limits,
  fileFilter(req, file, cb) 
    var filetypes = /jpeg|jpg|png|gif/;
    const mimetype = filetypes.test(file.mimetype);
    const extname = filetypes.test(
      path.extname(file.originalname).toLocaleLowerCase()
    );
    if (mimetype && extname) 
      return cb(null, true);
    
    cb(
      "Error: File upload only supports the following filetypes - " + filetypes
    );
  ,
).single("image");

function imageParse(req, res, next) 
  upload(req, res, function (err) 
    if (err) 
      return res.status(422).json( error: err.message );
    
    next();
  );


module.exports = imageParse;

组件路线

require("dotenv").config();
const express = require("express");
const logic = require("../logic");
const auth = require("./auth");
const imageParse = require("../utils/imageParse");
const literals = require("../i18n/literals");

const router = express.Router();

const  research_newPost_messageConfirm  = literals;

router.post("/lost", auth, imageParse, (req, res) => 
  console.log(req.buffer);
  const 
    file:  buffer ,
    body:  userId, category, name, age, city, description, lang ,
   = req;
(async () => 
    try 
      const user = await logic.newResearch(
        buffer,
        userId,
        category,
        name,
        age,
        city,
        description,
        lang
      );
      res.status(201).json( message: research_newPost_messageConfirm[lang] );
     catch ( message ) 
      res.status(400).json( error: message );
    
  )();
);

module.exports = router;

组件逻辑接口

const streamifier = require("streamifier");
const cloudinary = require("cloudinary").v2;
const  Research  = require("../models");
const 
  CLOUDINARY_API_KEY,
  CLOUDINARY_SECRET_KEY,
  CLOUDINARY_NAME,
 = require("../utils/config");
const  logic_newPost_messageError = require("../i18n/literals");

const logic = 
newResearch(buffer, userId, category, name, age, city, description, lang) 
    try 
      return (async () => 
        cloudinary.config(
          cloud_name: CLOUDINARY_NAME,
          api_key: CLOUDINARY_API_KEY,
          api_secret: CLOUDINARY_SECRET_KEY,
        );

        const image = await new Promise((resolve, reject) => 
          const uploadStream = cloudinary.uploader.upload_stream(
            (err, image) => 
              if (err) throw new LogicError("Image could not be uploaded");
              resolve(image);
            
          );
          streamifier.createReadStream(buffer).pipe(uploadStream);
        );
        const newPost = await Research.create(
          owner: userId,
          category,
          name,
          age,
          city,
          description,
          image: image.secure_url,
        );

        if (!newPost) throw new Error(`$logic_newPost_messageError[lang]`);
        console.log("api", newPost);
        return newPost;
      )();
     catch (error) 
      console.log(error);
    
  ,
;

module.exports = logic;

【问题讨论】:

【参考方案1】:

不是req.buffer

req.file.buffer

【讨论】:

以上是关于Multer - 无法读取未定义的属性“缓冲区”的主要内容,如果未能解决你的问题,请参考以下文章

Nextjs - multer - TypeError:无法读取未定义的属性“传输编码”

Multer 返回 req.file 未定义

无法读取未定义类型错误的属性“推送”:无法读取未定义错误的属性“推送”

带有 Ionic 4 的 SQLite?无法读取未定义类型错误的属性“then”:无法读取未定义的属性“then”

`无法读取未定义的属性(读取'组件')`

NextJS:未捕获的类型错误:无法读取未定义的属性(读取“属性”)