从Firebase列表中删除/更新一个项目

时间:2017-04-11 00:50:40

标签: firebase firebase-realtime-database ionic2 angularfire2

我正在使用带有AngularFire2和Firebase的Ionic2。我在Firebase中的节点下有一个列表/数组。这些项目添加了push()功能。以下是结构。

node:
     -KhPSh52vq2m23le1qZ5: "value1"
     -KhPShqhHDVuxeUbryT7: "value2"
     -KhPSijWf_EuwmCHPJjv: "value3"

现在我需要从列表中查询一个项目并将其删除。我正在使用以下代码进行查询并获取正确的FirebaseListObservable。

this.af.database.list(`node`, {
        query:{
          orderByValue: true,
          equalTo: 'value1'
        }
      });

之后如何从列表中删除/更新此单个项目

1 个答案:

答案 0 :(得分:2)

list observable公开了saving and removing items的几种方法。您应该调用remove,传递要删除的项目的键:

let list = this.af.database.list('node', {
  query:{
    orderByValue: true,
    equalTo: 'value1'
  }
});

// Query the list for the purposes of this example:
list.first().subscribe((items) => {

  // Remove the matching item:
  if (items.length) {
    list.remove(items[0].$key)
      .then(() => console.log('removed ' + items[0].$key))
      .catch((error) => console.log(error));
  }
});
相关问题