Volodymyr Humeniuk
Volodymyr Humeniuk

Reputation: 3801

Find object in Firebase by one value

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.

Upvotes: 0

Views: 1432

Answers (2)

Volodymyr Humeniuk
Volodymyr Humeniuk

Reputation: 3801

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

Upvotes: 0

Frank van Puffelen
Frank van Puffelen

Reputation: 599776

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:

Upvotes: 2

Related Questions