在ionic中,如何读取和显示来自Firestore的信息

时间:2019-07-13 14:51:28

标签: firebase ionic-framework google-cloud-firestore ionic4

在ionic中,我想从firestore的特定字段(如Name)中获取并显示信息,但问题在于它也显示了其他文档的字段Names。

ngOnInit(){
   this.authService.readc().subscribe(data => {

   this.datas = data.map(e => {
     return {
     Name: e.payload.doc.data()['Name'],
    };
  })
    console.log(this.datas);
  });
}
}

name() {

    var counter = this.firestore.doc(`info/${this.authService.userDetails().uid}`);
    counter.set({
      Name: this.Name
    })
  }

在authService中

readc() {
    return this.firestore.collection('info').snapshotChanges();
}

1 个答案:

答案 0 :(得分:0)

也许是这样?

home.page.ts

export class HomePage {

  names: any;

  constructor(auth: AuthService) {
    auth.readc().subscribe(data => {
      this.names = data.map(person => ({ name: person.name }));
    });
  }
}

auth.service.ts

export class AuthService {

  people: any;

  constructor(db: AngularFirestore) {
    this.people = db.collection('people').valueChanges();
  }

  readc(){
    return this.people;
  }
}

查看angularfire docs,了解有关在Firebase中使用“收藏夹和文档”的更多详细信息。

希望这会有所帮助。