Find object in Firebase by one value

允我心安 提交于 2019-12-08 11:35:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!