通过一个值在 Firebase 中查找对象 [英] Find object in Firebase by one value

查看:31
本文介绍了通过一个值在 Firebase 中查找对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下一个数据库列表(用户名 + 用户 ID).

如何通过用户 ID 查找对象并更改其密钥(用户名)?

我在 Angular 5 中使用 AngularFire2.

解决方案

您可以使用如下查询通过其值查找子节点:

var users = firebase.database().reference("usernames");var query = users.orderByValue().equalTo("Sk6I..."ltA2");

通过将侦听器附加到此查询,您将能够找到与 UID 匹配的用户(或任何用户",因为技术上可能有更多具有相同值的键)的引用和键.

但是您不能重命名节点.您必须删除现有节点,然后创建一个新节点.有关更多信息,请参阅:

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.

解决方案

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 中查找对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆