First of all, I hope anyone could help adjust my code line using document reference as below.
As you can see from my firebase console below, my FaceEmotion-id document has Faceemotion and Timestamp field.
Using my codes below, everytime user gets different emotion, the firestore will overwrite the emotions. Hence I couldnt see what are the different emotions user get when using my app and the different time he/she has used my app.
I need to prevent overwriting for the two fields mentioned above. Please help me. What I want is to add fields in the document not overwriting it.
One more, I have no idea how to generate different id for the document named FaceEmotion-id. I actually had referenced the document beforehand using Uid as you can see from my code below. But for the subdocument in the "Result" collection. I cant seem to find a way to make it unique.
This is my codes:
public static final String TAG = "TAG";
FirebaseAuth fAuth;
FirebaseFirestore fStore;
String userID;
String FaceEmotion;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detection);
fAuth = FirebaseAuth.getInstance();
fStore = FirebaseFirestore.getInstance();
StartQues = (Button) findViewById(R.id.view_question);
StartQues.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String Emotion = EmotionType.toString();
userID = fAuth.getCurrentUser().getUid();
FaceEmotion = "FaceEmotion";
DocumentReference documentReference = fStore.collection("users").document(userID).collection("Result").document("FaceEmotion-id");
//DocumentReference documentReference = fStore.collection("users").document(userID).collection("FaceEmotion").document(Emotion);
Map<String,Object> user = new HashMap<>();
user.put("FaceEmotion",Emotion);
user.put("timestamp", FieldValue.serverTimestamp());
documentReference.set(user).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.d(TAG, "onSuccess: Data has been saved "+ userID);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d(TAG, "onFailure: " + e.toString());
}
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…