Issue
I have implemented a code to create a collection named "Users". the code is running properly but is not saving my data in the database of my console.
Here is my code
user_profile.putFile(imageUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> uploadtask) {
String download_url = uploadtask.getResult().getDownloadUrl().toString();
if (uploadtask.isSuccessful()){
Map<String,Object> userMap = new HashMap<>();
userMap.put("name",user);
userMap.put("image",download_url);
firebaseFirestore.collection("Users")
.document(user_id).set(userMap)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(RegistrationActivity.this, "Uploaded Successfully", Toast.LENGTH_SHORT).show();
SendToMain();
progressBar.setVisibility(View.INVISIBLE);
}
});
} else {
Toast.makeText(RegistrationActivity.this, "error: "+uploadtask.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
I don't know what's going wrong and what's causing the problem. please help me for this. I am new to firebase by the way.
Solution
sometimes It would be better to allow Firebase store to auto create meaningful documentID for you.
can you try using add() function like below.
firebaseFirestore.collection("Users").add(UserMap)
.addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
Log.d(TAG, "DocumentSnapshot written with ID: " + documentReference.getId());
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.w(TAG, "Error adding document", e);
}
});
Answered By - Muhammad Waqas Bhati
Answer Checked By - Mary Flores (JavaFixing Volunteer)