0

I am using Ionic2 with AngularFire2 and Firebase. I have a list/Array under a node in Firebase. The items are added with push() function. Following is the structure.

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

Now I need to query one item from the list and delete it. I am doing the query with the following code and getting correct FirebaseListObservable.

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

After this how to remove/update this single item from the list

1 Answer 1

2

The list observable exposes several methods for saving and removing items. You should call remove, passing the key of the item to be removed:

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)); } }); 
Sign up to request clarification or add additional context in comments.

5 Comments

It did not work. As I know parameter for remove function should be the key. Here value1 is not the key, its the value.
If I use subscribe won't it trigger every time there is a change in the list? I want to avoid this scenario.
It's an example. There is insufficient context in your question to infer how or what you intend to do. You only asked how it could be done.
The solution is working. Thanks for that. What I am asking is that is there any othere way beside using subscribe as it triggers everytime there is a change in the list. What additional information is required to understand the scenario. I have explained the complete thing I am trying to do.
The subscribe is not part of the solution; it's only there to show how the item that's being removed is obtained and how its key is is passed to remove.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.