0

I created a Python function named get_maps() that has 2 arguments, year and category and I would like to access it from JS, customizing those 2 arguments. However the code below returns an error:

list is not specified

function onSubmit(e) {
  e.preventDefault()
  
  if (year.value === "" || cat.value === "") {
    msg.textContent = "Please fill out the fields"
    msg.classList.add("error");
  } else {
    msg.textContent = ""
    msg.classList.remove("error");
    
    $.ajax({
      type: "POST",
      url: "~/mapsinterpeter.py",
    }).done(function(o) {
      list = get_maps(year.value, mapdict.cat.value)
    });
    
    year.value = ""
    cat.value = ""
    
    console.log(list)
  }
}
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • The AJAX call is asynchronous, so you're calling `console.log(list)` ***before*** the `list` variable has been declared and set. Put the `console.log()` line (and any logic which depends on it) within the `done()` code block, or put it in a function which is invoked from `done()`. See the duplicates for more information on this. – Rory McCrossan Jul 09 '22 at 15:55
  • Also, ***always*** declare your variables. Don't use lazy declaration. JS may technically allow it, but it's bad practice. – Rory McCrossan Jul 09 '22 at 15:56
  • Also, also, your AJAX call seems pretty redundant as you post no data to it, and don't work with the response in your JS...? – Rory McCrossan Jul 09 '22 at 15:57
  • Im not too sure what data means, working witht the response will come after I get the response – Bluestonex64 Jul 09 '22 at 16:27

0 Answers0