简体   繁体   English

如何从Firebase检索UID并将其添加到Firebase DB的子级

[英]How to retrieve UID from firebase and add it to child of firebase DB

first off I am very new to firebase database. 首先,我是Firebase数据库的新手。 I am looking into how do i retrieve the current UID (that is currenty logged into) from firebase and insert it into child of firebase along with a value all through the oncreate. 我正在研究如何从Firebase检索当前的UID(当前已登录),并将其连同值一起通过oncreate插入到Firebase的子级中。

I'm trying to push mycurrent UID and it's value to Firebase. 我正在尝试将当前的UID推送给Firebase。 I am having trouble pushing it to firebase. 我在将其推向火力基地时遇到了麻烦。 Here is my firebase structure: 这是我的firebase结构:

(eg, UID Fo2SfEfryWEjmxr4yIQwM5LQQx2 and the value '720' in fire base) (例如,UID Fo2SfEfryWEjmxr4yIQwM5LQQx2和基于火力的值'720'

After trying out, i am still unable to get the following code to retrieve UID. 试用之后,我仍然无法获得以下代码来检索UID。 I'm having trouble with (new ValueEventListener() as well as this line. 我在使用(new ValueEventListener()以及这行(new ValueEventListener()遇到麻烦。

Map<String, Queue> data = postSnapshot.getValue(new GenericTypeIndicator<Map<String, Queue>>() {});

Here is my onCreate for reference 这是我的onCreate供参考

    mRef = new Firebase("https://sunwayfinancequeue.firebaseio.com/");
    mRef.child("UserQueue");
    mRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {
            for (DataSnapshot postSnapshot : snapshot.getChildren()) {
                Queue queue = postSnapshot.getValue(Queue.class);
                System.out.println(Queue.getTotalqueue() + " - " + Queue.getServingqueue());
                Map<String, Queue> data = postSnapshot.getValue(new GenericTypeIndicator<Map<String, Queue>>() {});
                Map.Entry<String, Queue> entry = data.entrySet().iterator().next();
                String uid = entry.getKey();
                Log.d("Firebase UID ", uid);

            }
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {
            System.out.println("The read failed: " + firebaseError.getMessage());

        }
    });

Here is my constructor: 这是我的构造函数:

public class Queue {

private static double servingqueue;
private static double totalqueue;
private static String uid;

public Queue(Double servingqueue, Double totalqueue) {
    Queue.servingqueue = servingqueue;
    Queue.totalqueue = totalqueue;

}

public static double getServingqueue() {
    return servingqueue;
}

public static void setServingqueue(double servingqueue) {
    Queue.servingqueue = servingqueue;
}

public static double getTotalqueue() {
    return totalqueue;
}

public static void setTotalqueue(double totalqueue) {
    Queue.totalqueue = totalqueue;
}

public static String getUid() {
    return uid;
}

public static void setUid(String uid) {
    Queue.uid = uid;
    }
}

You are using the legacy Firebase Database SDK, 2.5.x . 您正在使用旧版Firebase数据库SDK2.5.x。 If you are just getting started with using the database, it would be better to use the new 9.xx SDK , especially if you are using Firebase Authentication from the new SDK. 如果您只是开始使用数据库,则最好使用新的9.xx SDK ,尤其是从新SDK使用Firebase身份验证时。 You will likely encounter problems if you use the old and new SDKs in the same build. 如果在同一版本中使用旧的和新的SDK,则可能会遇到问题。 See the Upgrade Guide . 请参阅《 升级指南》

You want to use Firebase authentication. 您要使用Firebase身份验证。 Check out this link to set up Firebase authentication for your Android App: 请查看此链接以为您的Android应用设置Firebase身份验证:

https://firebase.google.com/docs/auth/android/start/ https://firebase.google.com/docs/auth/android/start/

This link will help you manage the users and show you how to get the current user's UID: 该链接将帮助您管理用户,并向您展示如何获取当前用户的UID:

https://firebase.google.com/docs/auth/android/manage-users https://firebase.google.com/docs/auth/android/manage-users

Once you have Firebase Authentication set up you would use this piece of code to get the UID of the current user. 一旦设置了Firebase身份验证,就可以使用这段代码来获取当前用户的UID。

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
    String UID = user.getUid();
} else {
    // No user is signed in
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM