Ionic 4 Firestoe如何在数据中保存ID?

时间:2019-11-29 09:51:46

标签: angular ionic-framework google-cloud-firestore

我需要将文档ID保存在集合中。意味着当用户保存数据时,它还将保存文档ID和数据。谁能告诉我该怎么做。谢谢:)

  create() {
     // the user details are updated in firebase user list
     this.groupDetails = {
       groupname: this.Name,
       description: this.description,
       members: this.checked,
       messages: [],
       createdId: this.user.userId,
       createrName: this.user.username
     }
     console.log(this.groupDetails);
    // details are submitted to creatGroup function from groupProvider
     this.messageService.createGroup(this.groupDetails).then(resp => {
      console.log(resp);
      this.toast.show("Group Created");
      this.router.navigate(["/home"]);
     });

   }

  createGroup(groupDetails) {
    return this.angularFirestore.collection('Groups').add(groupDetails);
  }

1 个答案:

答案 0 :(得分:1)

如果有人有更好的答案,我现在就这样做,它也很棒

  createGroup(groupDetails) {
    return this.angularFirestore.collection('Groups').add(groupDetails).then(ref => {
      ref.set({ group_id: ref.id }, { merge: true }).then(() => {
      console.log("Group id is added");
      });
  })
}