サーバーへの画像ファイルのアップロードに問題があります。YouTubeでmulterに関するチュートリアルを視聴しましたが、チュートリアルとまったく同じことを行っていますが、何らかの理由でエラーが発生します:( "TypeError:Cannot read property '未定義のパス ')。私はエラーをグーグルで検索し、同じ問題を抱えている人を見つけ、彼らのようにそれを解決しようとしましたが、それは私にとってはうまくいきませんでした。
これは私のコードです:
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)の値をログに記録すると、そのタイプは「未定義」であると表示されます。これは、multerが画像ファイルを認識しなかったか、受信しなかったことを意味しているに違いありません。 multerがファイルを取得しないのは何が間違っているのですか?
回答 1 件
関連記事
- TypeError:未定義のプロパティ 'execute'を読み取れません[DiscordBot]
- TypeError:未定義のAPIのプロパティ 'ft'を読み取れません
- TypeError:未定義のプロパティ 'name'を読み取ることができません(create react app:Appjsで)
- TypeError:ReactJSフォームフィールドで未定義のプロパティ 'name'を読み取ることができません
- TypeError:未定義のプロパティ 'state'を設定できません
- TypeError:未定義のnodejs expressmvcのプロパティ 'render'を読み取れません
- TypeError:未定義のReactJSのプロパティ 'push'を読み取れません
- TypeError:> ServerResponsejsonで未定義のプロパティ 'get'を読み取れませんか?
- TypeError:ReactDataGridで未定義のプロパティ 'length'を読み取ることができません
目的地をに変更しました
./uploads
私にとってはうまくいきます