'Image uploaded through JavaScript not working

Here is my code

var upload_image="";
const image = document.querySelector("#image"); 

image.addEventListener("change",()=>{
    // console.log(image.value)
    const reader = new FileReader();
    // console.log(reader)
    reader.addEventListener("load",()=>{
        upload_image=reader.result;
    })
    reader.readAsDataURL(this.files[0]);
});

Output:

index.js:50 Uncaught TypeError: Cannot read properties of undefined (reading '0') at HTMLInputElement. (index.js:50:35)



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source