如何在Firebase身份验证和Firebase-实时数据库中获取相同的ID?

时间:2019-10-19 17:01:35

标签: java firebase firebase-realtime-database firebase-authentication

如何获取Firebase身份验证和Firebase实时数据库的相同ID? 当用户注册帐户时,他们将在数据库中获得其ID,但是后来我意识到ID是不同的吗?如何确保两个数据库都具有相同的ID? Different Id

//create customer
firebaseAuth.createUserWithEmailAndPassword(email, password)
        .addOnCompleteListener(RegisterActivity.this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if (task.isSuccessful()) {
                    authData = firebaseAuth.getUid();
                    String id = databaseReference.push().getKey();
                    Customer info = new Customer(id, name, email, address, telephone_number);
                    FirebaseDatabase.getInstance().getReference("Customer")
                            .child(Objects.requireNonNull(FirebaseAuth.getInstance().getCurrentUser()).getUid())
                            .setValue(info).addOnCompleteListener(new OnCompleteListener<Void>() {
                        @Override
                        public void onComplete(@NonNull Task<Void> task) {
                            regProgressBar.setVisibility(View.VISIBLE);
                            Toast.makeText(RegisterActivity.this, "Registration Complete", Toast.LENGTH_SHORT).show();
                            startActivity(new Intent(getApplicationContext(), MainActivity.class));
                        }
                    });
                }
            }
        });


 FirebaseDatabase.getInstance().getReference("Customer")
                                            .child(Objects.requireNonNull(FirebaseAuth.getInstance().getCurrentUser()).getUid())

我尝试以此替换上面的代码

 FirebaseDatabase.getInstance().getReference("Customer")
                                            .child(id)

same id where i want to achieve

我具有相同的ID,但是随后遇到另一个无法显示我的个人资料活动的问题

  

java.lang.NullPointerException:尝试从空对象引用上的字段'java.lang.String com.example.gerobokgo.Model.Customer.name'中读取

所以我决定改回第一个编码。

1 个答案:

答案 0 :(得分:0)

没关系,我已经解决了这个问题

只需删除-5184169415607375486 -2082614198258325552 -1084866128895283137 并更改String id = databaseReference.push().getKey();

FirebaseDatabase.getInstance().getReference("Customer").child(Objects.requireNonNull(FirebaseAuth.getInstance().getCurrentUser()).getUid()).setValue(info).addOnCompleteListener(new OnCompleteListener<Void>()

就是这样!