【问题标题】:Image Upload Error: TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined图片上传错误:TypeError [ERR_INVALID_ARG_TYPE]:\"path\" 参数必须是字符串类型。收到未定义
【发布时间】:2022-09-29 19:36:56
【问题描述】:

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

TypeError [ERR_INVALID_ARG_TYPE]:\"path\" 参数必须是字符串类型。收到未定义


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:\\VS\\MERN clones\\SocialApp\\server\\node_modules\\multer\\storage\\disk.js:37:28
    at DiskStorage.filename [as getFilename] (F:\\VS\\MERN clones\\SocialApp\\server\\index.js:50:5)
    at F:\\VS\\MERN clones\\SocialApp\\server\\node_modules\\multer\\storage\\disk.js:34:10
    at DiskStorage.destination [as getDestination] (F:\\VS\\MERN clones\\SocialApp\\server\\index.js:47:5)
    at DiskStorage._handleFile (F:\\VS\\MERN clones\\SocialApp\\  code: \'ERR_INVALID_ARG_TYPE\'
}
  • 在您的声明cb(null, req.body.name) 中,req.body.name 未定义。

标签: node.js reactjs express file-upload multer


【解决方案1】:

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

【讨论】:

  • 虽然您的陈述可能是正确的,但错误不是由此引起的。另外,请花更多的精力来格式化您的答案。句子以大写字母开头,引用路径的....
  • 错误消息对于所讨论的错误非常清楚。报告的“TYPE”不是参数预期的“字符串”类型。
【解决方案2】:

你解决了这个问题吗?我有同样的问题 :(

【讨论】:

    猜你喜欢
    • 2021-01-22
    • 2022-01-12
    • 2021-09-08
    • 2020-12-30
    • 2021-12-12
    • 2020-09-21
    • 2020-09-16
    • 1970-01-01
    相关资源
    最近更新 更多