java - 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:
感谢您的帮助。
解决方案
在这里,您是如何获得 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();
}
}
推荐阅读
- node.js - 无法在项目 npm 上执行目标 org.codehaus.mojo:exec-maven-plugin:1.3.2:exec (npm run build)
- javascript - 如何创建一个列出数字的字符串?
- laravel - 使用 where 约束区分 Laravel 中多条路线的最佳方法是什么?
- python - 如何避免 Django 的 `|linebreaksbr` 和 Bootstrap 风格冲突?
- php - 组合数据类型以传递给@include
- regex - 如何删除 \ 除非后跟“?
- python-3.x - 如何从数据集中删除图像及其标签
- ruby-on-rails - 在单个 POST HTTP 请求中发送一组不同的哈希值
- java - 返回泛型类型时出现 ClassCastException
- wordpress - Nginx 位置指令中子目录的正则表达式或通配符