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

Posted

技术标签:

【中文标题】Nextjs - multer - TypeError:无法读取未定义的属性“传输编码”【英文标题】:Nextjs - multer - TypeError: Cannot read property 'transfer-encoding' of undefined 【发布时间】:2021-02-15 08:53:34 【问题描述】:

我是 Nextjs 的新手,尝试通过 multer 上传文件并收到错误 TypeError: Cannot read property 'transfer-encoding' of undefined。该路由受到保护,因此必须添加身份验证中间件,该中间件工作正常,但我无法弄清楚在这种情况下如何使用 multer。

这里是代码。

multer.js

import multer from "multer";
import path from "path";

// Multer config
export default multer(
  storage: multer.diskStorage(),
  fileFilter: (req, file, cb) => 
    let ext = path.extname(file.originalname);
    if (ext !== ".jpg" && ext !== ".jpeg" && ext !== ".png") 
      cb(new Error("File type is not supported"), false);
      return;
    
    cb(null, true);
  ,
);

cloudinary.js

import cloudinary from "cloudinary";

cloudinary.config(
  cloud_name: process.env.CLOUDINARY_CLOUD_NAME,
  api_key: process.env.CLOUDINARY_API_KEY,
  api_secret: process.env.CLOUDINARY_API_SECRET,
);
export default cloudinary;

requireLogin.js(身份验证中间件)

import jwt from "jsonwebtoken";

const requireLogin = (fn) => 
  return (req, res) => 
    const  authorization  = req.headers;
    if (!authorization) 
      return res.status(403).json( error: "Restricted access" );
    
    try 
      const decode = jwt.verify(authorization, process.env.JWT_SECRET);
      req.user = decode;
      return fn(req, res);
     catch (err) 
      console.log(err);
      return res.status(500).json( error: "Something went wrong" );
    
  ;
;

export default requireLogin;

pages/api/products.js

import Product from "../../models/Product";
import User from "../../models/User";
import initDB from "../../utils/db";
import requireLogin from "../../utils/requireLogin";
import cloudinary from "../../utils/cloudinary";
import upload from "../../utils/multer";

initDB();

export default async (req, res) => 

  const createProduct = requireLogin((request, response) => 
    upload.single("image")(async (req, , err) => 
      try 
        let user = await User.findById(req.user._id);
        if (user.role !== "admin") 
          return response.status(403).json( error: "You must be an admin to add product" );
        

        const result = await cloudinary.v2.uploader.upload(req.file.path);
        req.body.image = result.secure_url;
        req.body.cloudinary_id = result.public_id;

        const product = new Product(
          name,
          price,
          description,
          image: req.body.image,
          cloudinary_id: req.body.cloudinary_id,
        );

        await product.save();
        response.json(product);
       catch (err) 
        console.log(err);
      
    );
  );

  switch (req.method) 
    case "POST":
      return createProduct(req, res);    
  
;

【问题讨论】:

【参考方案1】:

解决了。我不知道我必须将bodyParser设置为false

pages/api/products.js

export const config = 
  api: 
    bodyParser: false,
  ,
;

export default // rest of the code

【讨论】:

以上是关于Nextjs - multer - TypeError:无法读取未定义的属性“传输编码”的主要内容,如果未能解决你的问题,请参考以下文章

multer创建文件夹,如果不存在

使用 multer 和 express 上传图片

是否可以在路由器内部使用multer?

multer详解

NodeJS Multer 不工作

使用 multer 上传多个文件,但来自不同的字段?