0

I have a Json array, that I'm parsing into a javascript object array.

btn.addEventListener("click", function() {
  var request = new XMLHttpRequest();
  request.open('GET', 'http://localhost:8084/RestandJson/api/Person');
  request.onload = function() {
    var data = JSON.parse(request.responseText);
    // renderhtml(data);
    addPerson(data);
  };
  request.send();
});

I have made an addPerson function to add a user-defined object into the list.

addPerson = (data) => {
  var fName = document.getElementById("fname");
  var lName = document.getElementById("lname");
  var age = document.getElementById("age");
  var id = data.pop()+1;
  const person = {fName, lName, age, id};
  data.add(person);
}

when the function is called I get 'addPerson is not defined' as an error. Why is this?

here is my entire file:

var btn = document.getElementById("btn");
var root = document.getElementById("root");


btn.addEventListener("click", function() {
    var request = new XMLHttpRequest();
    request.open('GET', 'http://localhost:8084/RestandJson/api/Person');
    request.onload = function(){
      var data = JSON.parse(request.responseText);
      //renderhtml(data);
      addPerson(data);

    };
    request.send();

});



renderhtml = (data) => {
let htmlString = " ";

for(i = 0; i < data.length; i++){
    htmlString += "<p> " + data[i].fName +" " +  data[i].lName + "</p>"
}



addPerson = (data) => {
    var fName = document.getElementById("fname");
    var lName = document.getElementById("lname");
    var age = document.getElementById("age");
    var id = data.pop()+1;
    const person = {fName, lName, age, id};
    data.push(person);

}



root.insertAdjacentHTML('beforeend', htmlString);
}
3
  • same error occurs Commented Sep 22, 2018 at 13:24
  • 1
    The error means that addPerson() is defined in a scope such that it's not accessible from the XHR callback. Without seeing more code it's impossible to be more precise. Commented Sep 22, 2018 at 13:25
  • Thank you for your comment, I have edited the code, so we can see the entire file Commented Sep 22, 2018 at 13:28

2 Answers 2

4

It's because you didn't close your renderHTML function properly, it should be like this:

renderhtml = (data) => {
  let htmlString = " ";

  for(i = 0; i < data.length; i++){
      htmlString += "<p> " + data[i].fName +" " +  data[i].lName + "</p>"
  }
} // <-- Add closing bracket to renderHTML function here

addPerson = (data) => {
  var fName = document.getElementById("fname");
  var lName = document.getElementById("lname");
  var age = document.getElementById("age");
  var id = data.pop()+1;
  const person = {fName, lName, age, id};
  data.push(person);
}
Sign up to request clarification or add additional context in comments.

Comments

1

Try moving the whole addPerson function outside the renderhtml function, most probably because that addPerson function is inside the renderhtml function

2 Comments

Welcome to StackOverflow. While this answer is good, you could make it great by adding the revised code, see other answers for what I mean by this. As it stands, it is more of a comment than an answer. As this is a "coding" site code is always good and does not always have to be large, just illustrative.
My apologies, thanks for letting me know @MarkSchultheiss :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.