NSFletcher
NSFletcher

Reputation: 31

Error thrown when using “return” on Submit event function call

I’m in the process of creating a simple application in Electron that allows you to add tasks to a list. However, it shouldn’t allow you to add empty tasks.

The following script is a minimum, verifiable version of what I’m using inside the Renderer JS file.

const taskForm = document.querySelector('form');
taskForm.addEventListener('submit', addTask);

function addTask(event)
{
  const formInput = taskForm.querySelector('input[type="text"]');
  const taskName = formInput.value;
  formInput.value = "";

  if (!taskName || taskName.length === 0)
  {
    console.log("Empty"); 
    return; //It doesn't seem to like this return. No idea why.
  }

 //Task element is created and added

  event.preventDefault();
}

I’ve also put together a JSFiddle, for those who want to mess around with the issue in code: https://jsfiddle.net/bts22a34/ It seems to throw an issue here too, despite being outside Electron.

Whenever it hits the return function, it throws the following error:

"Error: No such module: atom_renderer_v8_util"

From adding a breakpoint just before the "if" statement, it seems to successfully complete the return, hit the end of the function, and then throw the error.

If I send in a non-empty string, it skips past the if statement successfully and completes the function with no error.

Upvotes: 2

Views: 90

Answers (1)

NSFletcher
NSFletcher

Reputation: 31

From messing around with it again just now, it seems to be an issue with regards to “event.preventDefault” not being run if you go through the if statement.

const taskList = document.querySelector('div');
const taskForm = document.querySelector('form');
taskForm.addEventListener('submit', addTask);

id = -1;

function addTask(event)
{
  const formInput = taskForm.querySelector('input[type="text"]');
  const taskName = formInput.value;
  formInput.value = "";

  if (!taskName || taskName.length === 0)
  {
    console.log("Empty"); 
  }

  else
  {
    //Task element is created and added
  }

  event.preventDefault();
}

JSFiddle: https://jsfiddle.net/bts22a34/

Upvotes: 1

Related Questions