Jump to content

WHy does removing eventListener cause an error?


Hom35tead

Recommended Posts

Ok So i just wanted to remove the event listener from my uploadFiles() function in my js.  Because I want the function to kick off only when I click that button and not any other button in the form as well ass preventing the listener from running twice if somebody closes the overlay and goes back into it without refreshing.  Here is my code.  Why do I keep receiving a promise error.

It is pretty vanilla stuff.  Something I am just not seeing.The code that worls with the eventListener.
`

/* ----- Upload File / Image -----*/
function uploadFile(uid) {
 const url = 'process.php';
  const form = document.querySelector('form');
  //let uid = document.getElementById('upid').value;
 
    var data = {
      auploadid: "1",
      auserid: "1",
    };

  const btn = document.getElementById('newFiles');
 
 
  //console.log("Run Onlick");
  form.addEventListener('submit', e => {
    //console.log("Run Heard The Click");
      e.preventDefault();
 
      const files = document.querySelector('[type=file]').files;
      const formData = new FormData();
 
      for (let i = 0; i < files.length; i++) {
          let file = files[i];
          //console.log(uid);
          formData.append('files[]', file);
          formData.append('uploadid', uid);
      }
 
      fetch(url, {
          method: 'POST',
          body: formData
      }).then(response => {
          return response.text();
      }).then(data => {
          console.log(data);
      });
  });
  return;
}

And this is the code that throws the error. Again, all I am trying to do is remove the event Listener.

/* ----- Upload File / Image Without Listener-----*/
function uploadFiles(uid) {
  const url = 'process.php';
  //const form = document.querySelector('form');
  const form = document.getElementById("uploadForm");
 
  //e.preventDefault();

  const files = document.querySelector('[type=file]').files;
      const formData = new FormData();
 
      for (let i = 0; i < files.length; i++) {
          let file = files[i];
          //console.log(uid);
          formData.append('files[]', file);
      }
  formData.append('uploadid', uid);
  fetch(url, {
    method: 'POST',
    body: formData
}).then(response => {
    return response.text();
}).then(data => {
    console.log(data);
});
return;
}


 

 

Link to comment
Share on other sites

/* ----- Upload File / Image ----- */
function uploadFile(uid) {
    const url = 'process.php';
    const form = document.querySelector('form');
    
    form.onsubmit = function(e) {
        e.preventDefault();

        const files = document.querySelector('[type=file]').files;
        const formData = new FormData();

        for (let i = 0; i < files.length; i++) {
            let file = files[i];
            formData.append('files[]', file);
            formData.append('uploadid', uid);
        }

        fetch(url, {
            method: 'POST',
            body: formData
        }).then(response => {
            return response.text();
        }).then(data => {
            console.log(data);
        });
    };
    
    return;
}

Instead of using the addEventListener method for the form submission, you can directly bind the function to the form's onsubmit?

Link to comment
Share on other sites

2 hours ago, Strider64 said:

Instead of using the addEventListener method for the form submission, you can directly bind the function to the form's onsubmit?

Using those handlers is frowned upon in modern Javascript. For one, because it's not actually a binding process: you're literally assigning a function to that property, which means you can't assign a second one to it as well.

Link to comment
Share on other sites

18 hours ago, requinix said:

Using those handlers is frowned upon in modern Javascript. For one, because it's not actually a binding process: you're literally assigning a function to that property, which means you can't assign a second one to it as well.

Ok so here is the deal.  If I packege all this code up into a single flat file in html.  It works or at least says successful and does not throw an error.  Whether the actual file moves or not I dont  yet.  However as soon as I break it down into Form on PHP file, Javascript in a js file referenced from PHP.  It stops working and gives me an error. "Uncaught type (in promise)"

 

The whole reason I am trying to avoid the EventListener is because if the user opens the overlay (which kicks off the function) and then closes it without uploading a file.  When they reopen that form it listens for the 'submit' event two times.  Uploads two files and console logs them separately, so I know it is running the actual function 2 times.  I like the onlick or onsubmit binding (false binding) as I can reuse this function over and over without it screwing up my other stuff.  Like I can use the same function on multiple parts of the application without havng to have a listener on every page.  Is there a better approach?

-Home3s

Link to comment
Share on other sites

22 hours ago, requinix said:

If you're having a problem with an error then it's typically very helpful to others if you mention what that error is.

Sorry, i completely left that out.  It is "uncaught Type (in promise) error.  Pretty much all it says.

Link to comment
Share on other sites

5 hours ago, Hom35tead said:

Sorry, i completely left that out.  It is "uncaught Type (in promise) error.  Pretty much all it says.

"Uncaught TypeError"? What is the uncaught TypeError it speaks of? There should be more to the message than just that...

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.