【问题标题】:Cloudinary upload works locally but error on HerokuCloudinary 上传在本地工作,但在 Heroku 上出错
【发布时间】:2022-01-17 16:55:45
【问题描述】:

在我的 Heroku 部署中尝试将图像上传到 Cloudinary 时,我收到错误 (node:22) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'path' of undefined,但当我在本地执行时,它工作得很好。

错误发生的地方:

const upload = require("../utils/multer");
const cloudinary = require("../utils/cloudinary");

//Add Question Set
router.post("/add", upload.single("questionSetImage"), async (req, res) => {
    //Get the Author Email
    const token = req.cookies.jwt;
    const decoded = jwt.verify(token, process.env.TOKEN_SECRET);
    let user = await User.findById(decoded._id);

    //Validate Data
    const { error } = questionSetValidation(req.body);
    if (error) return res.status(400).json(error.details[0].message);

    console.log(req.file.path); //error occured here
    const imageUpload = await cloudinary.uploader.upload(req.file.path);
    console.log(imageUpload);

    //Create New Question
    const questionSet = await new QuestionSet({
        questionSet: req.body.questionSet,
        theme: req.body.theme,
        visible: req.body.visible,
        totalQuestion: req.body.totalQuestion,
        author: user._id,
        questionSetImage: imageUpload.secure_url,
        cloudinaryID: imageUpload.public_id,
    });
    try {
        const saveQuestionSet = await questionSet.save();
        res.status(200).json(saveQuestionSet);
    } catch (err) {
        res.status(400).json(err);
    }
});

我的 Multer 代码:

const path = require("path");

//Multer config
module.exports = 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 代码:

const cloudinary = require("cloudinary").v2;
require("dotenv").config();

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

module.exports = cloudinary;

我不知道为什么它不仅仅适用于 Heroku 部署。它说错误与路径有关,但是在本地路径工作正常。我不知道文件是否正确上传。

编辑:原来它甚至不能在本地工作。我得到与TypeError: Cannot read property 'path' of undefined 相同的错误

【问题讨论】:

  • 看起来req.file 未定义。您如何向/add 提出请求?
  • 在我的邮递员表单数据中,我为 questionSetImage 添加了一个文件:imgur.com/a/Q6AK8Ii。当我尝试 console.log(req.file) 它确实说未定义
  • 在意识到我的一些验证不正确后,我让它在本地工作。我尝试将相同的图像(454kb)上传到 Heroku,然后得到相同的未定义错误。然后我尝试上传一个较小尺寸的图像(75kb)并且它以某种方式工作。用原始图像再次尝试,现在它也可以了。
  • 很高兴听到这个消息!如果您能够清楚地解释问题是什么以及如何解决它,请随时在下面添加self-answer。如果您选择这样做,请阅读How to Answer,因为我们的目标是为将来的其他用户提供有用的信息。或者,如果您不确定问题是什么,但您不再需要答案,您可以选择删除您的问题。

标签: node.js mongodb express heroku cloudinary


【解决方案1】:

在意识到我的一些验证不正确后,我让它在本地工作。我尝试将相同的图像(454kb)上传到 Heroku,然后得到相同的未定义错误。然后我尝试上传一个较小尺寸的图像(75kb)并且它以某种方式工作。用原始图像再次尝试,现在它也可以工作了

【讨论】:

    猜你喜欢
    • 2019-10-19
    • 2020-09-06
    • 1970-01-01
    • 2014-01-02
    • 1970-01-01
    • 1970-01-01
    • 2021-04-26
    • 2013-03-29
    • 2015-02-07
    相关资源
    最近更新 更多