Nodejs module running before the Eventlistener

Asked

Viewed 29 times

2

I’m creating a module for node and by performing some tests with the eventListener of the kind change in a field of the kind file, the module is executed before a frame is inserted into the field.

exports.preview = (fileInput, whereToSee) => {
    if (fileInput && fileInput[0]) {
        let reader = new FileReader();
        reader.onload = (event) => {
            whereToSee.attr('src', event.target.result);
        }
        reader.readAsDataURL(fileInput[0]);
     } else {
        console.error('None!!'); // Mensagem de teste
    }

}

The Code of Testing:

var upload = require('../index.js');

const file = document.querySelector('input[type="file"]');

file.addEventListener('change', upload.preview(this, document.querySelector('#blah')));

Just when I open the browser the message None!! is displayed, ie the Event change is being activated even before a file is selected.

I’m using the webpack to create the bundle.js from the archive main.js which is the archive where the test codes are found:

webpack main.js Bundle.js

1 answer

2


You have to use the concept of high order Function, that is to return a function to the .addEventListener call, a callback in this case.

It could be so:

exports.preview = (fileInput, whereToSee) => {
    return function(){
        if (fileInput && fileInput[0]) {
            let reader = new FileReader();
            reader.onload = (event) => {
                whereToSee.attr('src', event.target.result);
            }
            reader.readAsDataURL(fileInput[0]);
         } else {
            console.error('None!!'); // Mensagem de teste
        }
    }
}

that way when you invoke the function as you have in your code, as an argument from addEventListener, it captures the variables in the callback scope that you return. This yes (the callback) will be called when there is change.

  • but that’s not the same as assigning a function to exports ?

  • 1

    @Rafaelacioly no. You need to pass a function to Exports as you have, and that function returns another, which is the callback that will be used. Because the addEventListener needs a function, and as you have it, it runs, if runs and the addEventListener does not even receive a/callback function in the arguments.

  • 1

    that solved! thanks! but now I’m having another problem too! rs when I pass the this as a parameter of upload.preview(this, ...) the file input object is not returned, but an object (I don’t know which exactly), as it could to pass the own file as parameter without having to select it again?

  • 1

    @Rafaelacioly it would not be case to pass file in time of this?

  • but I need to get the element after it’s changed change, if I pass the file the element is still empty.

  • @Rafaelacioly but it is kept, just because it receives a new value/file do not lose the reference. When the change was called, the input will have changed and you can read what you need from it.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.