简体   繁体   中英

Typescript Error Property 'members' does not exist on type '{}'

This piece of code has been working well until a few days ago when I upgraded angularfire and firebase: From "angularfire2": "5.0.0-rc.4" , to "angularfire2": "^5.1.0" , from "firebase": "4.8.0" , to "firebase": "^5.5.7" :

this.dataProvider.getGroup(this.groupId).snapshotChanges().subscribe((group) => {
  this.group = group.payload.val();
  this.groupMembers = null;
  // Get group members
  if (group.payload.val().members) {
    group.payload.val().members.forEach((memberId) => {
      this.dataProvider.getUser(memberId).snapshotChanges().subscribe((member) => {
        this.addOrUpdateMember(member);
      });
    });
    // Get user's contacts to add
    this.dataProvider.getCurrentUser().snapshotChanges().subscribe((account) => {
      if (account.payload.val().contacts) {

        //Delete log
        console.log('My contacts',account.payload.val().contacts);
        for (var i = 0; i < account.payload.val().contacts.length; i++) {
          this.dataProvider.getUser(account.payload.val().contacts[i]).snapshotChanges().subscribe((contact) => {
            // Only contacts that are not yet a member of this group can be added.

              contact = { $key: contact.key, ...contact.payload.val()};
              //Delete log
              console.log('Is contact',contact,' already added?',this.isMember(contact));
            if (!this.isMember(contact))
              this.addOrUpdateContact(contact);
          });
        }
        if (!this.contacts) {
          this.contacts = [];
        }
      } else {
        this.contacts = [];
      }
    });
  }
  this.loadingProvider.hide();
});

This is the getGroup() in the provider:

getGroup(groupId) {
    return this.angularfire.object('/groups/' + groupId);
}

It is bringing this error everywhere there is payload.val() .someProperty in the project.

Here is how I get the data from firebase.

firebase.database().ref('/MyCustomFolder').once("value").then(snapshot => {
  let data = snapshot.val();
  console.log(data.customers);
});

I suggest trying the following:

this.dataProvider.getGroup(this.groupId).snapshotChanges().subscribe((group) => {
  let data = group.val();
  let payload = data.payload;
  if (payload.members) {   
      /* do something */
  }

This is what I had to do to get it working:

this.dataProvider.getGroup(this.groupId).snapshotChanges().subscribe((group) => {
  let data: any = group.payload.val();
  if (data.members) {   
      /* do something */
  }

So apparently I had to include : any . Again, given that is was working before I made the upgrade - the experts will tell us more of the cause.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM