首页 > 解决方案 > Firestore - 如何知道文档中的 id?

问题描述

如何从 firestore - android studio 中的文档中知道 ID?

在这段代码中,你可以看到,我是如何创建一个新帐户的,这里我想用用户 id 创建新文档,但他没有用这个 id 创建,他放了 other-one。

if (!confirmarCampos(nome,email,eNdata,id,password)){

        fAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {

            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {

                if (task.isSuccessful()) {
                    pessoa.put("nome", nome);
                    pessoa.put("email", email);
                    pessoa.put("ano", eNdata);
                    pessoa.put("idBicicleta", id);
                    pessoa.put("distancia", 0);
                    pessoa.put("pontos", 0);

                    db.collection("pessoa")
                            .document(user_id) //here i put the reference from user id
                            .set(pessoa)
                            .addOnCompleteListener(new OnCompleteListener<Void>() {
                                @Override
                                public void onComplete(@NonNull Task<Void> task) {

                                    if(task.isSuccessful()){
                                        Toast.makeText(Registar.this,"Perfil Criado com Sucesso", Toast.LENGTH_SHORT).show();


                                    }else{
                                        String erro = task.getException().getMessage();
                                        Toast.makeText(Registar.this,"ERRO :" + erro, Toast.LENGTH_SHORT).show();
                                    }
                                }
                            });
                    Intent iConfirmar = new Intent(Registar.this, Login.class);
                    startActivity(iConfirmar);
                    finish();
                }else{
                   Toast.makeText(Registar.this, "O email já existe", Toast.LENGTH_LONG).show();
                }
            }
        });
    }

在这里您可以看到新用户的 id。

在此处输入图像描述

在这里,您可以看到新文档中的 id 与用户 id 不同。因此,我无法使用文档中的值,因为我想将值设置为 profile.xml 。

文档 ID: 在此处输入图像描述 感谢您的帮助。

标签: javaandroidfirebasegoogle-cloud-firestore

解决方案


在这里,您是如何获得 user_id 的?

尝试像这样获取用户ID,

if (task.isSuccessful()) {
    // Sign up in success, update UI with the signed-in user's information
    FirebaseUser user = mAuth.getCurrentUser();
    if (user != null) {
        user_id = user.getUid();               
    }           
}

推荐阅读