亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

multer npm:TypeError:無法讀取未定義的屬性“path”

multer npm:TypeError:無法讀取未定義的屬性“path”

LEATH 2023-11-12 22:28:17
我在將圖像文件上傳到我的服務器時遇到問題,無論出于何種原因,我都收到錯誤:(&ldquo;TypeError:無法讀取未定義的屬性'路徑'&rdquo;)。我在谷歌上搜索了這個錯誤,發現有些人遇到了同樣的問題,我試圖像他們一樣解決它,但它對我不起作用。這是我的代碼:const multer = require('multer');const storage = multer.diskStorage({? destination: function(req, file, cb) {? ? cb(null, './public/images/profilePictures');? },? filename: function(req, file, cb) {? ? cb(null, new Date().toISOString() + file.originalname);? }});const fileFilter = (req, file, cb) => {? // reject a file? if (file.mimetype === 'image/jpg' || file.mimetype === 'image/png') {? ? cb(null, true);? } else {? ? cb(null, false);? }};const upload = multer({? storage: storage,? limits: {? ? fileSize: 1024 * 1024 * 5? },? fileFilter: fileFilter});app.use(express.static('public'))映像架構和模型:const imageSchema = new mongoose.Schema({? ? profilePicture: String})const Image = new mongoose.model('Image', imageSchema)我的帖子路線:app.post('/changeProfilePic', upload.single('profilePicture'), function(req, res, next){? ? console.log(req.file);? ?const newImage = new Image({? ? ? ?profilePicture: req.file.path? ?})? ?newImage.save()})我的html上傳表格:<form action="/changeProfilePic" method="POST" enctype = "multipart/form-data">? ? ? <input type="file" name="profilePicture" placeholder="Image" />? ? ? <button class="btn btn-light btn-lg" type="submit">Upload</button>? ? </form>當我記錄 (req.file) 的值時,它說它的類型是&ldquo;未定義&rdquo;,所以這一定意味著 multer 沒有識別甚至沒有收到圖像文件。我做錯了什么,Multer 沒有得到文件?
查看完整描述

1 回答

?
侃侃爾雅

TA貢獻1801條經驗 獲得超16個贊

我更改了目的地以適合我./uploads



查看完整回答
反對 回復 2023-11-12
  • 1 回答
  • 0 關注
  • 170 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號