【发布时间】: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