Typescript错误属性'members'在类型'{}'上不存在

时间:2018-11-09 07:57:24

标签: typescript firebase ionic3 angularfire2 angularfire

直到几天前,当我升级了angularfire和firebase时,这段代码一直运行良好: 从"angularfire2": "5.0.0-rc.4""angularfire2": "^5.1.0", 从"firebase": "4.8.0""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();
});

这是提供者中的getGroup()

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

在项目中存在 payload.val() .someProperty的任何地方都将带来此错误。

2 个答案:

答案 0 :(得分:0)

这是我从Firebase获取数据的方式。

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

我建议尝试以下方法:

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

答案 1 :(得分:0)

这是我要做的事情:

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

因此,显然我必须包括: any。同样,鉴于在我进行升级之前它一直在工作-专家会告诉我们更多原因。