Marven Wilson S Donque
Marven Wilson S Donque

Reputation: 275

destructuring returns undefined

I am trying to pass an object as a parameter to a function, then that function will push the properties of the object to an array, then a method will display the added values to the console. it is successfully creating the instance of object that I pass, but the properties are undefined. where did I go wrong? here is my code:

let myApp =(function() {
    let personProps = {
       firstName: null,
       lastName: null,
       Age: null,
    }
    let university = {
       numberOfSutdents : null,
       numberOfStaff : null,
       sections : ['Gumamela', 'St. Anne', 'St. John'],
       students : [],
       staff: []
    }

    let staff = Object.create(personProps);
    staff.hireDate = null;
    staff.rule = null;

    let faculty = Object.create(staff);
    faculty.subjectTeaching = null;
    faculty.NumberOfSectionsCovered = null;

    let student = Object.create(personProps);
    student.section = null;
    student.year = null;
    student.yeadEnrolled = null;

    // Checks if staff is a prototype of university
    let h = Object.getPrototypeOf(student) === personProps ? true : false;

    // CMDS
    let addStudent = ({_fn , _ln, _age}) => {
       let _student = [
          student.firstName = _fn,
          student.lastName = _ln,
          student.Age = _age
        ]
        university.students.push(_student)
     }

    //Query
    let getStudents = () => {
        return console.log(university.students)
      }

     return {  
        addStudent,
        getStudents
     }
})();


myApp.addStudent({fisrtName: 'marven', lastName: 'donque', Age: 22})
myApp.addStudent({fisrtName: 'john', lastName: 'tompson', Age: 23})

myApp.getStudents();


// it returns 2 instance of array, but with 3 undefined properties.

Upvotes: 1

Views: 3075

Answers (2)

vertika
vertika

Reputation: 1424

Your parameter names should match :

So you have to change it to :

let addStudent = ({firstName , lastName, Age}) => {
   let _student = [
      student.firstName = firstName,
      student.lastName = lastName,
      student.Age = Age
    ]
    university.students.push(_student)
 }

And while calling :

myApp.addStudent({fisrtName: 'marven', lastName: 'donque', Age: 22});

This will work completely fine.

Upvotes: 3

kemotoe
kemotoe

Reputation: 1777

As stated in the comments when destructuring an object, the variable name must exactly match the name of the key in the object. Also there was a typo when you created the students fisrtName should be firstName and the return when console.log isn't needed.

let myApp =(function() {
    let personProps = {
       firstName: null,
       lastName: null,
       Age: null,
    }
    let university = {
       numberOfSutdents : null,
       numberOfStaff : null,
       sections : ['Gumamela', 'St. Anne', 'St. John'],
       students : [],
       staff: []
    }

    let staff = Object.create(personProps);
    staff.hireDate = null;
    staff.rule = null;

    let faculty = Object.create(staff);
    faculty.subjectTeaching = null;
    faculty.NumberOfSectionsCovered = null;

    let student = Object.create(personProps);
    student.section = null;
    student.year = null;
    student.yeadEnrolled = null;

    // Checks if staff is a prototype of university
    let h = Object.getPrototypeOf(student) === personProps ? true : false;

    // CMDS
    let addStudent = ({firstName , lastName, Age}) => {
       let _student = [
          student.firstName = firstName,
          student.lastName = lastName,
          student.Age = Age
        ]
        university.students.push(_student)
     }

    //Query
    let getStudents = () => {
        console.log(university.students)
      }

     return {  
        addStudent,
        getStudents
     }
})();


myApp.addStudent({firstName: 'marven', lastName: 'donque', Age: 22})
myApp.addStudent({firstName: 'john', lastName: 'tompson', Age: 23})

myApp.getStudents();


// it returns 2 instance of array, but with 3 undefined properties.

Upvotes: 0

Related Questions