【问题标题】:JavaScript: Accessing object inside an array inside object using multerJavaScript:使用 multer 访问对象内部数组中的对象
【发布时间】:2021-09-11 01:14:42
【问题描述】:

这是我的 multer 函数 -

const storage = multer.diskStorage({
  destination: (req, file, callback) => {
    let type = req.params.type;
    let path = `./data/${type}`;
    fs.mkdirsSync(path);
    callback(null, path);
  },
  filename: (req, file, cb) => {
    const ext = mime.extension(file.mimetype);
    const random = Math.floor(Math.random() * (9999 - 1000 + 1)) + 1000;
    cb(null, `${Date.now()}-${random}.${ext}`);
  },
});

这是路由器 - router.post("/", auth, multer({ storage }).fields([{ name: 'image', maxCount: 1 }, { name: 'video', maxCount: 1 }]), addMovie); 这是我在console.log(req.files) 时得到的结果-

[Object: null prototype] {
  image: [
    {
      fieldname: 'image',
      originalname: '����� ����.png',
      encoding: '7bit',
      mimetype: 'image/png',
      destination: './data/undefined',
      filename: '1631293039713-7613.png',
      path: 'data\\undefined\\1631293039713-7613.png',
      size: 13133
    }
  ],
  video: [
    {
      fieldname: 'video',
      originalname: 'file_example_MP4_480_1_5MG.mp4',
      encoding: '7bit',
      mimetype: 'video/mp4',
      destination: './data/undefined',
      filename: '1631293039720-3601.mp4',
      path: 'data\\undefined\\1631293039720-3601.mp4',
      size: 1570024
    }
  ]
}

我正在发送图片和视频。我想访问对象内的字段。 我不知道该怎么做。

试过了 - req.files.image[0] 这就是错误 - Property 'image' does not exist on type '{ [fieldname: string]: File[]; } | File[]'. Property 'image' does not exist on type 'File[]'.ts(2339)

【问题讨论】:

    标签: javascript typescript multer


    【解决方案1】:

    你可以试试(req as any).files.image[0]req 打字有点麻烦:(

    【讨论】:

      猜你喜欢
      • 2013-04-28
      • 1970-01-01
      • 2019-07-29
      • 2020-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多