PLATFORM
  • Tails

    Create websites with TailwindCSS

  • Blocks

    Design blocks for your website

  • Wave

    Start building the next great SAAS

  • Pines

    Alpine & Tailwind UI Library

  • Auth

    Plug'n Play Authentication for Laravel

  • Designer comingsoon

    Create website designs with AI

  • DevBlog comingsoon

    Blog platform for developers

  • Static

    Build a simple static website

  • SaaS Adventure

    21-day program to build a SAAS

๐Ÿ•ต๏ธโ€โ™‚๏ธ Typescript Multer Error : Property 'location' does not exist on type 'File'.

๐Ÿ•ต๏ธโ€โ™‚๏ธ Typescript Multer Error : Property 'location' does not exist on type 'File'.

Written by Noah on Oct 31st, 2021 ใƒป Views ใƒป Report Post

Typescript Multer Error

Multer ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์‚ฌ์šฉํ•˜๋Š” ํ”„๋กœ์ ํŠธ์— AWS์˜ multerS3 ๋ฅผ ์ด์šฉํ•œ๋‹ค๋ฉด ๋งˆ์ฃผ์น  #typescript ์—๋Ÿฌ๋‹ค.

์ฐพ๋Š” ๋ฐฉ๋ฒ•์€ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์–ด๋–ป๊ฒŒ ์ •์˜๋ฌ๋Š”์ง€ ์ฐพ์•„๋ณด๋ฉด ๋œ๋‹ค.


const upload = multer({
  storage: multerS3({
    s3: new AWS.S3(),
    bucket: "project",
    key(req, file, cb) {
      cb(null, `original/${Date.now()}_${path.basename(file.originalname)}`);
    },
  }),
  limits: { fileSize: 20 * 1024 * 1024 },
});

router.post("/icon", upload.single("image"), async (req, res, next) => {
  User.update(
    { icon: req.file.location.replace(/\/original\//, "/thumb/") },
    // ์ด๋ถ€๋ถ„์—์„œ ์—๋Ÿฌ๊ฐ€ ๋‚˜๊ฒŒ๋˜๋Š”๋ฐ file์˜ ํƒ€์ž…์ด ์–ด๋–ป๊ฒŒ ์ •์˜๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ด๋ณด์ž
    { where: { id: req.body.id } }
  );
  res.json(req.file.location.replace(/\/original\//, "/thumb/"));
});

ctrl + ํด๋ฆญํ•ด์„œ ํƒ€์ž…์ •์˜๋ฅผ ๋ณด๋Ÿฌ๊ฐ€์ž

Request๋Š” Express์˜ ํƒ€์ž… ์ •์˜์ด๊ณ  ๊ทธ์•ˆ์— file ์ธ์ž๋Š” Multer๋กœ ์ •์˜๋˜์–ด์žˆ๋‹ค (IDE๊ฐ€ ๋น„์ฅฌ์–ผ์ŠคํŠœ๋””์˜ค๋ผ๋ฉด ๊ฐ–๋‹ค ๋Œ€๊ธฐ๋งŒํ•ด๋„ ์ถ”๋ก ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.)

๋‚˜๋Š” upload ๋ถ€๋ถ„์—์„œ ๋ถ„๋ช…ํžˆ Express.Multer๋ฅผ multerS3๋กœ ํ™•์žฅํ–ˆ๋Š”๋ฐ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋Š” ๊ทธ๋ถ€๋ถ„๊นŒ์ง€ ์ถ”๋ก ํ•ด์ฃผ์ง€ ๋ชปํ•œ๊ฒƒ์ด๋‹ค.

ํ•ด๊ฒฐ๋ฐฉ๋ฒ•


router.post("/icon", upload.single("image"), async (req, res, next) => {
  User.update(
    { icon: (req.file as Express.MulterS3.File).location.replace(/\/original\//, "/thumb/") },
    // req์˜ file ํƒ€์ž…์„ MulterS3.File๋กœ ์ •์˜ํ•ด์ฃผ์ž.
    { where: { id: req.body.id } }
  );
  res.json((req.file as Express.MulterS3.File).location.replace(/\/original\//, "/thumb/"));
});

๋น„๋‹จ Multer๋ฟ๋งŒ ์•„๋‹Œ ์—๋Ÿฌ๋ผ ํ•ญ์ƒ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ type ํŒŒ์ผ์„ ๊นŠ๊ฒŒ ๋“ค์—ฌ๋‹ค๋ณด๋ฉฐ

ํƒ€์ž…์ •์˜๋ฅผ ํŒŒ์•…ํ•˜๊ณ  ํ•„์š”ํ•˜๋‹ค๋ฉด ์ˆ˜์ •ํ•ด ํƒ€์ž… ๋ถˆ์ผ์น˜ ์˜ค๋ฅ˜๋ฅผ ๋ฏธ์—ฐ์— ๋ฐฉ์ง€ํ•˜๋Š” ์Šต๊ด€์ด ์ค‘์š”ํ•˜๋‹ค

Comments (0)

loading comments