-1

I have Cannot read property 'convertIntoCorrectDate' of undefined error.

I think when I call this.convertIntoCorrectDate, this refers to objectResa and not my component. I tried to call myComp.convertIntoCorrectDate instead but it triggers error too (myComp.convertIntoCorrectDate is not a function).

Do you know how I can call my function ?

class myComp extends Component{
    constructor(props){
      //        this.convertIntoCorrectDate = this.convertIntoCorrectDate.bind(this);
    }

    fillBooking(responseJson){
        var reservations = responseJson.reservations;
        reservations.forEach(function(objectResa) {
              var aResaEvent = {
                     id: objectResa.resa.ID,
                     start: this.convertIntoCorrectDate(objectResa.resa.start), 
                     end: this.convertIntoCorrectDate(objectResa.resa.end)
              };
              console.log(aResaEvent);
        });

    }

    convertIntoCorrectDate(date){
        // code to be written
        return "0";
    }
```
droledenom
  • 157
  • 1
  • 10

1 Answers1

2

If you change your fillBooking(responseJson) function to:

fillBooking = (responseJson) => {

and the forEach to:

reservations.forEach((objectResa) => {

(to arrow functions), the this should point to the scope you desire.

Yannick K
  • 4,887
  • 3
  • 11
  • 21