图片上传错误:TypeError [ERR_INVALID_ARG_TYPE]:“路径”参数必须是字符串类型。收到未定义

我正在尝试通过反应应用程序将图像上传到我的 node.js 服务器。为此,我使用了 Multer 和 path npm 包。但是每当我上传它都会显示错误:

TypeError [ERR_INVALID_ARG_TYPE]:“路径”参数必须是字符串类型。收到未定义


app.use("/images", express.static(path.join(__dirname, "/public/images")));

const storage = multer.diskStorage({
  destination: (req, file, cb) => {
    cb(null, "/public/images");
  },
  filename: (req, file, cb) => {
    cb(null, req.body.name);
  },
});

const upload = multer({ storage: storage });
app.post("/api/upload", upload.single("file"), (req, res) => {
  try {
    return res.status(200).json("File uploded successfully");
  } catch (error) {
    console.error(error);
  }
});

完全错误:

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined
    at new NodeError (node:internal/errors:371:5)
    at validateString (node:internal/validators:119:11)     
    at Object.join (node:path:429:7)
    at F:VSMERN clonesSocialAppservernode_modulesmulterstoragedisk.js:37:28
    at DiskStorage.filename [as getFilename] (F:VSMERN clonesSocialAppserverindex.js:50:5)
    at F:VSMERN clonesSocialAppservernode_modulesmulterstoragedisk.js:34:10
    at DiskStorage.destination [as getDestination] (F:VSMERN clonesSocialAppserverindex.js:47:5)
    at DiskStorage._handleFile (F:VSMERN clonesSocialApp  code: 'ERR_INVALID_ARG_TYPE'
}
stack overflow Image Upload Error: TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined
原文答案

答案:

作者头像

您提供的路径是错误的。根据文件夹的位置,它应该是 ./public/images 或 ../public/images 。

相关问题