TypeError:无法读取未定义的属性(读取' diskstorage')
我是新来的。
我想使用multer将文件保存到我的服务器。
import { Router } from "express";
import { multer } from "multer";
import { ffmpeg } from "fluent-ffmpeg";
import { path } from "path";
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(
null,
"C:Users/ksw/Desktop/back/src/routes/uploads/"
);
},
filename: (req, file, cb) => {
cb(null, `${Date.now()}_${file.originalname}`);
},
fileFilter: (req, file, cb) => {
const ext = path.extname(file.originalname);
if (ext !== ".mp4") {
return cb(res.status(400).end("only jpg, png, mp4 is allowed"), false);
}
cb(null, true);
},
});
const upload = multer({ storage: storage }).single("file");
这是我的代码。
当我尝试运行服务器时,会显示此错误。
var Storage = _multer.multer.diskstorage({ ^
typeError:无法读取未定义的属性(读取“ diskstorage”),
我认为存储目的地起初是相对的问题。 上面的代码包含绝对路径(之前只是'/uploads/')。都不正确。 (存在文件夹)
我在Web上找不到类似的问题。可能是我缺少一些基本的东西。 我可以尝试解决这个问题?谢谢您的阅读。
I am new here.
I want to use multer to save files to my server.
import { Router } from "express";
import { multer } from "multer";
import { ffmpeg } from "fluent-ffmpeg";
import { path } from "path";
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(
null,
"C:Users/ksw/Desktop/back/src/routes/uploads/"
);
},
filename: (req, file, cb) => {
cb(null, `${Date.now()}_${file.originalname}`);
},
fileFilter: (req, file, cb) => {
const ext = path.extname(file.originalname);
if (ext !== ".mp4") {
return cb(res.status(400).end("only jpg, png, mp4 is allowed"), false);
}
cb(null, true);
},
});
const upload = multer({ storage: storage }).single("file");
this is my code.
when I try to run the server, this error shows up.
var storage = _multer.multer.diskStorage({
^
TypeError: Cannot read properties of undefined (reading 'diskStorage')
I thought there was a problem with the storage destination which was relative at first.
The code above contains the absolute path (it was just '/uploads/' before). Neither are correct. (The folder exists)
I can't find similar problems on web. It's probably me missing some basic stuff.
What can I try to solve this issue? Thank you for reading.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我知道那太晚了,但是。我以前遇到过同样的问题,对我有什么帮助,我只是改变了进口风格。因此,您的行:
从“路径”;
我更改为
import path = require('path');
(更多的Express样式)和之后,它有效。I know that is kinda too late, but. I had the same problem before and what is helped to me that I just changed style of import. So, you have line:
import { path } from "path";
I changed it to
import path = require('path');
(more of express style) and after that it works.