问题
I have next database list (usernames + user id).
How can i find object by user id and change his key (username)?
I'm using AngularFire2 with Angular 5.
回答1:
You can find a child node by its value with a query like this:
var users = firebase.database().reference("usernames");
var query = users.orderByValue().equalTo("Sk6I..."ltA2");
By attaching a listener to this query you'll be able to find the reference and the key of the user (or "any users", since technically there may be more keys with the same value) matching the UID.
But you can't rename a node. You'll have to remove the existing node, and create a new one. For more on this see:
- Firebase: Update key?
- Firebase API for moving a tree branch from a collection to another one
- Is it possible to rename a key in the Firebase Realtime Database?
回答2:
Solution for my case:
this.db.list('usernames', ref => ref.equalTo(uid)).remove() // Remove old value
this.db.list('usernames', ref => ref.equalTo(uid)).set(
username, uid
) // Create new value
来源:https://stackoverflow.com/questions/49459891/find-object-in-firebase-by-one-value