Home>

I want to create a chat app using ionic x firebase.
An error will occur in the following part.

I was able to confirm that the data could be obtained from the firebase DB.
I am in trouble because I do not know the cause of the error.

Thank you.

[Error message]
ERROR TypeError: Cannot read property'rooms' of undefined
at room.page.ts: 33
at DataSnapshot.js: 126
at LLRBNode.push ../node_modules/@firebase/database/dist/cjs/src/core/util/SortedMap.js.LLRBNode.inorderTraversal (SortedMap.js: 170)
at LLRBNode.push ../node_modules/@firebase/database/dist/cjs/src/core/util/Sorte

export class RoomPage implements OnInit {
  rooms = new Array ();
  constructor (public navCtrl: NavController) {}
  async ngOnInit () {
    firebase.auth (). onAuthStateChanged ((user) =>{
      if (user) {
        firebase.database (). ref ('chatrooms /'). on ('value', function (resp)
        {
          if (resp) {
            console.log ("firebase.database (). Ref ()");
            //this.rooms = new Array ();
            resp.forEach (function (child snapshot)
            {
              const room = childsnapshot.val ();
              room.key = childsnapshot.key;
              this.rooms.push (room);// * Where the error occurred
              return false;
            });
          }
        });
      } else {
        this.navCtrl.navigateRoot ('signin');
      }
    });
  }