问题
So I have this code:
firebase.database().ref('putninalog/'+ NadiiIndeksNaloga()).remove();
This removes etc. [0] and all data connected to that index in database (it doesn't matter which number exactly). When I remove that index how do I update all other indexes to start from 0.
Example:
[0] [1] [2] [3]
I remove: [0]
This stays:
[1] [2] [3]
I want this:
[0] [1] [2]
回答1:
There is no way to remove an item at an index and then update all subsequent indexes in one operation. You will have to:
- Read the entire parent node into your application code.
- Remove the item from the array in your application code.
- Write the entire array back to the database.
And if other users may be manipulating the array at the same time, you'll have to do this using a transaction to prevent the updates from conflicting with each other.
To find out more about a better way to deal with lists of data in Firebase, I recommend reading the documentation on appending items to a list of data and the blog post Best Practices: Arrays in Firebase.
回答2:
I did some digging, I cant do it the way I wanted. I realized it thanks to Frank's answer. I did it this way to find index of each item:
ref.on('value',function(snap)
{
snap.forEach(function(child)
{
console.log(child.ref_.path.pieces_[1]); // This will return mentioned [index] from database
});
});
来源:https://stackoverflow.com/questions/65615178/firebase-remove-with-javascript