TypeError:无法读取未定义的属性(读取' diskstorage')

发布于 2025-01-28 03:35:35 字数 1086 浏览 1 评论 0原文

我是新来的。

我想使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

洒一地阳光 2025-02-04 03:35:35

我知道那太晚了,但是。我以前遇到过同样的问题,对我有什么帮助,我只是改变了进口风格。因此,您的行:

从“路径”;

我更改为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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文