首页 > 解决方案 > 如何从离子3实时Firebase中的数据列表中删除特定数据

问题描述

我能够上传和获取数据,但我想使用一个按钮从数据列表中删除数据。下面是我的代码

ionViewDidLoad() {
    console.log('ionViewDidLoad PagesAllpatientsPage');
    this.ph.getPatientProfile(this.id).once('value', snapshot => {
      this.patientsList = [];
      console.log('sjiy')
      snapshot.forEach(snap => {
        this.patientsList.push({
          id: snap.key,
         name: snap.val().name,
         age: snap.val().age,
         gender: snap.val().gender,
         phonenumber: snap.val().phonenumber,
        });
        console.log(this.patientsList)
        return false
      });
    });

  }

  deletePatient(id:any,) {
    firebase.database().ref(`userProfile/${this.user.uid}/PatientInfo/${id}`).remove(id)
  }

我想要实现的只是通过单击按钮从 PatientInfo 中删除数据,还请提供在此处输入图像描述

<button ion-button block (click)="deletePatient(id)">Delete Record</button>

标签: typescriptfirebaseionic-frameworkfirebase-realtime-databaseionic3

解决方案


我知道这有点晚了,但它可能会帮助有需要的人。

/* this will help you get the ref of unique id of the current data */
uid = **apH....q1e2** in this case
let ref = this.afDatabase.database.ref('userProfile/' + uid + 'PatientInfo');
    
/* now to remove patient info */
ref.remove().then(data => {
console.log('Data removed successfully.');
})

推荐阅读