Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG 紀錄] 使用 POSTMAN PUT 上傳圖片檔案,無法從 req.files 取得資料 #46

Open
WilliamTsou818 opened this issue Sep 16, 2021 · 0 comments

Comments

@WilliamTsou818
Copy link
Owner

問題描述

目標:在 PUT /api/users/:id 新增圖片上傳功能
實作:

  • 引用 multer 作為 middleware 處理檔案上傳
  • 在 controller putUser 取得 req.files ,並用 if 判斷是否有檔案上傳
  • 若有檔案上傳,呼叫 imgur 套件上傳圖片至 imgur

問題:

  • 在取得 req.files 的值時,即使 POSTMAN 的 CONTENT-TYPES 設定正確,avatar 和 cover 屬性也有上傳圖片檔案,但依然取得了 [Object: null prototype] 的值

期望的行為

  • req.files 可以在有檔案上傳時正確取得 files 資訊

Screenshots

紀錄 BUG (multer Issue)
紀錄 BUG (postman 設置)

備註

目前已知事項:

  1. multer 處理上傳檔案的時候, file 的順序必須放在最後面

Note that req.body might not have been fully populated yet. It depends on the order that the client transmits fields and files to the server.

  1. multer 本身會回傳 cookies,而 POSTMAN 繼續使用的 old cookies 可能會導致圖片上傳失敗 (undefined)

解法:重開 POSTMAN 、重登入拿新 token 、重開一個 tab 打 PUT api

參考資料:

https://stackoverflow.com/questions/33829489/multer-req-file-is-undefined
https://stackoverflow.com/questions/35851660/multer-req-file-always-undefined
postmanlabs/postman-app-support#2602
https://www.npmjs.com/package/multer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant