Reputation: 730
Look I have created a Google Sign In method and after that a setup that should show up only for the first time when signin in in that screen he will enter some information about himself that will be uploaded into a document to firestore. That firestore document will have the same UID as the google sign in user. In order to figure it out if the user already has an account with some information provided I want to do a query for if a document already exists with that specific uid as their documentname.
FirebaseUser user = mAuth.getCurrentUser();
FirebaseFirestore db;
String currentID = mAuth.getCurrentUser().getUid();
// Checken ob User schon Dokument hat
db = FirebaseFirestore.getInstance();
db.collection("users")
.whereEqualTo("uid", currentID)
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() { // Add the listener callback
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if(task.isSuccessful()) {
// Check the size of the result to see if any matches were found
if(task.getResult().size() == 0) {
// No document exists containing the searched value
} else {
// A document already exists containing the searched value
hasDocument = true;
}
} else {
Log.e(TAG, "There was an error querying the documents.", task.getException());
}
}
});
if(hasDocument) {
SendUserToMainActivity();
} else {
SendUserToSetupActivity();
Toast.makeText(LoginActivity.this, "You must provide some information first", Toast.LENGTH_LONG).show();
}
I think my problem is that I am querying for the field and I don´t know how to query for the documentname instead of the field itself. How can I do that ?
This is the way the documents are structured:
Upvotes: 0
Views: 75
Reputation: 6919
Use the following code to check whether user already have document or no
FirebaseUser user = mAuth.getCurrentUser();
FirebaseFirestore db;
String currentID = mAuth.getCurrentUser().getUid();
//Checken ob User schon Dokument hat
db = FirebaseFirestore.getInstance();
db.collection("users")
.document(currentID)
.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
Log.d(TAG, "Document exists!");
} else {
Log.d(TAG, "Document does not exist!");
}
} else {
Log.d(TAG, "Failed with: ", task.getException());
}
}
});
Upvotes: 2