0

I have next database list (usernames + user id).

enter image description here

How can i find object by user id and change his key (username)?

I'm using AngularFire2 with Angular 5.

Volodymyr Humeniuk
  • 3,411
  • 9
  • 35
  • 70

2 Answers2

2

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:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
0

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
Volodymyr Humeniuk
  • 3,411
  • 9
  • 35
  • 70