通过一个值在 Firebase 中查找对象

编程入门 行业动态 更新时间:2024-10-06 16:17:24
本文介绍了通过一个值在 Firebase 中查找对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

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

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

我在 Angular 5 中使用 AngularFire2.

解决方案

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

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

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

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

Firebase:更新密钥?Firebase API 用于将树枝从一个集合移动到另一个集合是否可以重命名Firebase 实时数据库中的键?

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: 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?

这篇关于通过一个值在 Firebase 中查找对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-22 10:32:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1022319.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   Firebase

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!