从Firebase中删除行数据

时间:2018-05-24 06:27:20

标签: html angular typescript angularfire2 smart-table

我想在智能表中从Firebase中删除一个单击的行。我正在使用Angular 4。

智能表代码:

<ng2-smart-table [settings]="settings" [source]="bicycleData"
 (deleteConfirm)="onDeleteConfirm($event)">
</ng2-smart-table>

我的构造函数组件代码:

constructor(
    db: AngularFireDatabase, ) {
    this.bicyclesList = db.list('bicycle-list')
        .snapshotChanges()
        .map(changes => {
            return changes.map(c => ({
                    key: c.payload.key,
                    ...c.payload.val()
                }))
        });

    this.bicyclesList.subscribe((data) => {
        this.bicycleData = data;
    });
}

和component.ts代码:

settings = {
    delete : {
        deleteButtonContent: '<i class="nb-trash"></i>',
        confirmDelete: true,
    },
}
服务中的

onDeleteConfirm()函数和deleteEnquiry函数:

onDeleteConfirm(event) {
    console.log(event.data);
    if (window.confirm('Are you sure you want to delete?')) {
        this.deleteEnquiry(event.data);
        event.confirm.resolve();
    } else {
        event.confirm.reject();
    }
}

deleteEnquiry(data) {
    console.log(data.$key);
    this.db.list(`bicycle-list${data.$key}`).remove(data);
}

但是它一直在控制台中显示以下错误:

ERROR TypeError: Cannot read property 'list' of undefined

如何解决此错误?

1 个答案:

答案 0 :(得分:0)

deleteEnquiry中看起来像是一个错误。

根据docs应该是:

deleteEnquiry(data) {
  console.log(data.$key);
  this.db.list('bicycle-list').remove(data.$key);
}