dimanche 24 février 2019

Can console.log Object but cannot assign to var

So I have a section of code to read/parse a php file. The object looks like {plan{...}, catalog{...}}. I assign the first index of the object to a var and pass it into a new function

 if (this.readyState == 4 && this.status == 200) {
        var myObj = JSON.parse(this.responseText);
        let inputPlan = {};
        inputPlan = (myObj['plan']);
        getInputs(inputPlan);
}

In the function getInputs, I try to parse the "plan" object which is in the format of {student: "sam", age : "15", ...}.

 function getInputs(inputPlan){

        console.log(inputPlan["student"]);
        var student = inputPlan["student"];     
}

I can grab the "student" key of the object and I print it out to the console getting "sam". However, when I attempt to assign it to a variable, I get a cannot read property "student" of undefined. I'm not sure why this is happening as the console seems to be reading the specified key without a problem?

Thank you




Aucun commentaire:

Enregistrer un commentaire