使用push()方法创建没有唯一键的新子位置 [英] Using push() method to create new child location without unique key

查看:62
本文介绍了使用push()方法创建没有唯一键的新子位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Firebase Web应用程序中,当新用户使用他们的电子邮件注册时,我在其根目录下的电子邮件地址下创建一个节点

In my firebase web app when new users signup with their email i create a node under their email adress under the root ref

但是问题是使用push()方法将每个新用户添加到数据库中,每个用户都是使用唯一的键(如下所示)创建的

But the problem is using push() method to add every new user to the database every user is created with a unique key like below

-用户

---- ASFTU578FE

---------用户:user1@email.com

---------user: user1@email.com

---- FDWWE36S46

---------用户:user2@email.com

---------user: user2@email.com

---- WERSRTT23W

---------用户:user3@email.com

---------user: user3@email.com

现在我该如何为用户访问路径,因为我不知道将为每个新用户创建的唯一密钥

Now how can i access the path for a user since i dont know the unque key that will be created for every new user

有没有一种方法可以在没有唯一密钥的情况下推送新用户,但是我知道该密钥就像user.displayName或user.email

Is there a way to push new user without a unique key but the key i know like user.displayName or user.email

推荐答案

调用push会生成带有自动计算键的位置.

Calling push generates a location with an auto-calculated key.

要在自己确定密钥的路径上写子代,只需调用child("key").set(value).

To write a child at a path that you determine the key yourself, you simply call child("key").set(value).

如果您将Firebase身份验证用户存储在数据库中,则惯用的方法是将其存储在其uid下.

If you store Firebase Authentication users in your database, the idiomatic way is to store them under their uid.

var user = firebase.auth().currentUser;
var usersRef = firebase.database().ref("users");
if (user) {
  usersRef.child(user.uid).set({ 
    displayName: displayName,
    email: email,
    photoUrl: photoUrl,
    emailVerified: emailVerified
  });
}

这篇关于使用push()方法创建没有唯一键的新子位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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