How do I check if a child exists in the a branch in the database so that I can return values based on that child

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

How do I check if a child exists in the a branch in the database so that I can return values based on that child

问题

// 获取当前用户
user = FirebaseAuth.getInstance().getCurrentUser();
// 获取指向当前用户在 "Students" 分支下的引用
reff = FirebaseDatabase.getInstance().getReference().child("Students").child(user.getUid());

// 添加数据变化监听器
reff.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        // 获取学生信息
        name = dataSnapshot.child("name").getValue().toString();
        lastName = dataSnapshot.child("lastName").getValue().toString();
        studentID = dataSnapshot.child("studentID").getValue().toString();
        email = dataSnapshot.child("email").getValue().toString();
        password = dataSnapshot.child("password").getValue().toString();
        confirmPassword = dataSnapshot.child("password").getValue().toString();
        
        // 设置学生信息到界面
        inputStudentID.getEditText().setText(studentID);
        inputName.getEditText().setText(name);
        inputLastName.getEditText().setText(lastName);
        inputEmail.getEditText().setText(email);
        inputPassword.getEditText().setText(password);
        inputConfirmPassword.getEditText().setText(confirmPassword);
    }
});

// 如果以管理员身份登录,如何获取管理员分支并显示信息
// 使用 "Admin" 分支
adminReff = FirebaseDatabase.getInstance().getReference().child("Admin").child(user.getUid());
adminReff.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        // 获取管理员信息并显示
        // ...
    }
});

请注意,这里的代码只是一个翻译版本,并未对原始代码逻辑进行修改。如果你有进一步的问题或需要代码方面的帮助,请随时提问。

英文:

UI from using the code i mention
I have my database set up like this:

root

-Students
-user1
-name
-pass
-etc...
-user2
-Admin
-admin1

I want to check if the current user is an admin or a student so that i can go to a certain reference in the database

This is my code:

    user = FirebaseAuth.getInstance().getCurrentUser();
reff = FirebaseDatabase.getInstance().getReference().child("Students").child(user.getUid());
reff.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
name = dataSnapshot.child("name").getValue().toString();
lastName = dataSnapshot.child("lastName").getValue().toString();
studentID = dataSnapshot.child("studentID").getValue().toString();
email = dataSnapshot.child("email").getValue().toString();
password = dataSnapshot.child("password").getValue().toString();
confirmPassword = dataSnapshot.child("password").getValue().toString();
inputStudentID.getEditText().setText(studentID);
inputName.getEditText().setText(name);
inputLastName.getEditText().setText(lastName);
inputEmail.getEditText().setText(email);
inputPassword.getEditText().setText(password);
inputConfirmPassword.getEditText().setText(confirmPassword);
}

to get the students info I have to do

FirebaseDatabase.getInstance().getReference().child("Students").child(user.getUid());

but if I were to log on as an admin how would I go to that branch and display their info. I've already tried using an if statement with

dataSnapshot.child("Students").hasChild(user.getUID)

with the reference of

FirebaseDatabase.getInstance().getReference()

to see if the user logged in is in the student branch but that didn't work.

UI for both admin and student with the code in picture 3

Code for image 2

答案1

得分: 0

为了判断节点分支中是否存在子项,并根据此运行您的代码,您需要使用回调函数。

参考此文

英文:

In order to see if a child exists in the branch of a node and run your code based on that, you have to use a call back function.

See this article for reference

huangapple
  • 本文由 发表于 2020年4月9日 03:21:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/61108404.html
匿名

发表评论

匿名网友

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

确定