0

What I'm using

  • Angular
  • Firebase

What I'm trying to do

  • Check a firebase node to see if a user ID is true / exists

  • If it is, display a list of items from another node

Issues

  • I have these two pieces of functionality working independently
  • I can check a user node to see if they exist and write to the console
  • I can display a list of items
  • I cannot however, check the user and THEN display the list of items

This is the code that checks a 'members' node

This is what i use to see if the user key exists under members > project id

    var userId = firebase.auth().currentUser.uid;
    var pathToCheck = '/members/' + this.projectId + '/' + userId;

    firebase.database().ref(pathToCheck).once('value').then(function (snapshot) {
      if (snapshot.exists()) {
        console.log('hooplar!');
      } else {
        console.log('hmmm');
      }
    });
  }

This is how I display items

    this.activatedRoute.data.subscribe((
      data: { issueListData: any }) => {
      this.issuesList = data.issueListData;
    });  

What I'd like to happen

Ideally, this is the functionality I would like, but the following error is thrown:

ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'activatedRoute' of undefined

    var userId = firebase.auth().currentUser.uid;
    var pathToCheck = '/members/' + this.projectId + '/' + userId;

    firebase.database().ref(pathToCheck).once('value').then(function (snapshot) {
      if (snapshot.exists()) {
        this.activatedRoute.data.subscribe((
          data: { issueListData: any }) => {
          this.issuesList = data.issueListData;
        });
        console.log('hooplar!');
      } else {
        console.log('hmmm');
      }
    });
  }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
MegaTron
  • 3,133
  • 6
  • 28
  • 45

1 Answers1

1

You aren't binding this properly change your then to arrow function to have access to outside scope this. Or at the end of your then function by the closing brace add }.bind(this) I'm assuming this.activatedRoute is your class prop right? This inside your then function refers to database promise not your class

Vic
  • 404
  • 4
  • 7