Java Firebase云Firestore引用返回null
所以,我在firestore中创建了firebase用户配置文件,它将保存用户的数据,例如他们的姓名、电子邮件、电话号码和奖励积分。用户使用Google登录登录 这就是我将数据写入firestore的方式Java Firebase云Firestore引用返回null,java,android,firebase,google-cloud-firestore,Java,Android,Firebase,Google Cloud Firestore,所以,我在firestore中创建了firebase用户配置文件,它将保存用户的数据,例如他们的姓名、电子邮件、电话号码和奖励积分。用户使用Google登录登录 这就是我将数据写入firestore的方式 private void addNewUser() { FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); if (user != null) { String uid = user
private void addNewUser() {
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
String uid = user.getUid();
for (UserInfo profile : user.getProviderData()) {
// Id of the provider (ex: google.com)
String providerId = profile.getProviderId();
// UID specific to the provider
// Name, email address, and profile photo Url
String name = profile.getDisplayName();
String email = profile.getEmail();
Map<String, Object> newUser = new HashMap<>();
newUser.put("Nama", name);
newUser.put("Email", email);
// Add a new document with a generated ID
db.collection("users").document(uid).set(newUser);
}
}
}
在本例中,文档引用
curUser
,返回null。我不知道我做错了什么 您需要在文档参考
@Override
public void onStart() {
super.onStart();
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
String curUser = user.getUid();
DocumentReference documentReference = db.document(curUser);
documentReference.addSnapshotListener(this.getActivity(), new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(@javax.annotation.Nullable DocumentSnapshot documentSnapshot, @javax.annotation.Nullable FirebaseFirestoreException e) {
if (documentSnapshot.exists()) {
String userNama = documentSnapshot.getString(KEY_NAMA);
String userEmail = documentSnapshot.getString(KEY_EMAIL);
namaUser.setText(userNama);
emailUser.setText(userEmail);
}
}
});
} else {
Intent intent = new Intent(Home.this.getActivity(), LoginActivity.class);
startActivity(intent);
}
}
变,
DocumentReference documentReference = db.document(curUser);
对,
DocumentReference documentReference = db.collection("users").document(curUser);