에러일지/Vue.js
[Vue.js/에러일지] input type="file" / forEach not a function
레오나르도 다빈츠
2023. 1. 8. 16:48
에러
<input type="file" />로 받은 file 변수에 forEach not a function 스크립트 에러가 발생했다.
타입이 Array가 맞고 length까지 찍어봤지만 동일한 문제가 생겼다.
해결
[전]
handleUploadImage(e) {
let inputFile = e.target.files
let targetFile
// 여기서 에러 발생..!
inputFile.forEach((image) => {
targetFile.push(image)
})
}
[후]
1. let → const 변경
2. 파일을 전개연산자로 받았다.
handleUploadImage(e) {
const inputFile = [...e.target.files]
let targetFile
inputFile.forEach((image) => {
targetFile.push(image)
})
}
해결완료..🌈
개인적으로 공부한 내용을 정리하는 블로그로
잘못된 개념을 게시하지않도록 주의하고 있으나 오류가 있을 수 있습니다.