在Firebase实时数据库中创建相同的节点。

huangapple go评论82阅读模式
英文:

Make identical node in firebase realtime database

问题

假设有两个节点:

  1. 用户数据(在“用户数据”内部包含):

    唯一ID
    姓名:“abc”
    纬度:“123”
    经度:“456”

  2. 用户密钥(在“用户密钥”内部包含):

    唯一ID
    g:“awe46q”
    l
    0:123
    1:456

在这里,我想要使用相同的唯一ID保存“用户数据”和“用户密钥”,但我不知道如何做到这一点。

英文:

Suppose there are two nodes:

  1. user data (inside "user data" we have:)

    unique id
    name: "abc"
    latitude: "123"
    longitude: "456"

  2. user key (inside "user key" we have:)

    unique id
    g: "awe46q"
    l
    0:123
    1:456

Here I want to save both "user data" & "user key" with the same unique id. But I don't know how to do it.

答案1

得分: 0

> 我尝试过使用push()方法,但它生成了不同的唯一标识。

这是预期的行为,因为多次调用push()将每次生成一个新的随机标识符。

> 我是否需要使用Firebase身份验证来使“用户数据”和“用户密钥”的唯一标识类似。

当涉及到用户ID时,是的,最合适的解决方案是实施Firebase身份验证并使用在此过程中生成的UID。最后,您可以在需要的每个引用中简单地使用相同的UID,例如:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference userDataRef = rootRef.child("userData").child(uid); //一个引用
userDataRef.setvalue(yourObject);
DatabaseReference otherUserDataRef = rootRef.child("otherUserData").child(uid); //第二个引用
otherUserDataRef.setvalue(otherObject);
英文:

> I have tried the push() method but it generates different unique ids.

That's expected behavior since calling push() multiple times, will always generate each time a new random id.

> Will I have to use FirebaseAuth to make the unique id similar for "user data" & "user key".

When it comes to users IDs, yes, the most appropriate solution is to implement Firebase Authentication and use the UID that is generated in this process. In the end, you can simply use the same UID, in every reference that you need, for example:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference userDataRef = rootRef.child("userData").child(uid); //One reference
userDataRef.setvalue(yourObject);
DatabaseReference otherUserDataRef = rootRef.child("otherUserData").child(uid); //Second reference
otherUserDataRef.setvalue(otherObject);

huangapple
  • 本文由 发表于 2020年8月7日 15:42:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/63297384.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定