Reputation: 123
I am trying to update two fields in a firestore document but an error showed, I am not sure why the error stated no document is found when there are data in that document. The data was passed from the previous activity. The document was added to the database in the previous activity. How to solve this problem? Thank you in advanced.
E/Failed at update: NOT_FOUND: No document to update: projects/secretgarden-9defe/databases/(default)/documents/main/0M0F1Ug5TmcO3pVaZu4XMluGOty1/journal/0M0F1Ug5TmcO3pV
//this is the ReceivedActivity that received the data passed from previous activity
public class ReceivedActivity extends AppCompatActivity {
private Journal journal;
private String descText, detectedScoreText, sentencesToneText;
private TextView tonesText, sentencesText;
private FirebaseAuth mAuth;
private FirebaseFirestore db;
private String userId;
private CollectionReference userIdRef;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_emotion);
tonesText = (TextView) findViewById(R.id.toneText);
sentencesText = (TextView) findViewById(R.id.sentencesText);
//get info passed from other previous activity
journal = (Journal) getIntent().getSerializableExtra("journal");
descText = journal.getDescription();
mAuth = FirebaseAuth.getInstance();
db = FirebaseFirestore.getInstance();
userId = mAuth.getCurrentUser().getUid();
userIdRef = db.collection("main").document(userId).collection("journal");
if (userId == null) {
startActivity(new Intent(EmotionActivity.this, LoginActivity.class));
finish();
} else {
analyzeEmotion();
}
}
private void analyzeEmotion() {
//carry out some action
detectedScoreText = "some string";
sentencesToneText = "some string";
updateFirestoreEmotion();
}
private void updateFirestoreEmotion() {
userIdRef.document(journal.getId())
.update("detected", detectedScoreText, "sentences", sentencesToneText)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(EmotionActivity.this, "Updated detected tones :)", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(EmotionActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
Log.e("Failed at update", e.getMessage());
}
});
}
}
//this is the structure in firestore (I just want to update the "detected" & "sentences" field with data from ReceivedActivity)
//previous activity related info
private void addToFirestore() {
String userId = mAuth.getCurrentUser().getUid();
if (!userId.isEmpty()) {
CollectionReference userIdRef = db.collection("main").document(userId).collection("journal");
detectedScoreText = "Not analyzed yet";
sentencesToneText = "Not analyzed yet";
journal = new Journal(userId, title, descText, date, advice, answer, question, detectedScoreText, sentencesToneText);
userIdRef.add(journal).addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
Intent intent = new Intent(AddNoteActivity.this, ReceivedActivity.class);
intent.putExtra("journal", journal);
startActivity(intent);
finish();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
progressBar.setVisibility(View.GONE);
Log.e("Failed at saving note", e.getMessage());
showRedSnackbar(e);
}
});
} else {
progressBar.setVisibility(View.GONE);
Log.i("USER MIGHT NOT EXIST", "User ID is empty");
}
}
import com.google.firebase.firestore.ServerTimestamp;
import java.io.Serializable;
import java.util.Date;
public class Journal implements Serializable {
private String id;
private String title;
private String description;
private String advice;
private String answer;
private String question;
public String getDetected() {
return detected;
}
public void setDetected(String detected) {
this.detected = detected;
}
public String getSentences() {
return sentences;
}
public void setSentences(String sentences) {
this.sentences = sentences;
}
private String detected;
private String sentences;
@ServerTimestamp
private Date date;
public Journal(){}
public Journal(String id, String title, String description, Date date, String advice, String answer, String question, String detected, String sentences) {
this.id = id;
this.title = title;
this.description = description;
this.date = date;
this.advice = advice;
this.answer = answer;
this.question = question;
this.detected = detected;
this.sentences = sentences;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getAdvice() {
return advice;
}
public void setAdvice(String advice) {
this.advice = advice;
}
public String getAnswer() {
return answer;
}
public void setAnswer(String answer) {
this.answer = answer;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
}
Upvotes: 2
Views: 5730
Reputation: 4035
Since you are passing an object:
intent.putExtra("journal", journal);
I hope that your Journal
class implements Serializable
, which means:
//your class must implement Serializable
class Journal implements Serializable{
..........
}
UPDATE
You are not putting the correct id in your document field id
Do this:
......
......
journal = new Journal(userId, title, descText, date, advice, answer, question, detectedScoreText, sentencesToneText);
userIdRef.add(journal).addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
//get the reference of the document like this:
String documentID = documentReference.getId();
Intent intent = new Intent(AddNoteActivity.this, ReceivedActivity.class);
intent.putExtra("journal", journal);
//send it along with the object
intent.putExtra("documentID", documentID);
startActivity(intent);
finish();
.......
......
In the next activity get both the object and documentID:
private Journal journal;
private String documentID;
..........
//get them from intent
journal = (Journal) getIntent().getSerializableExtra("journal");
documentID = getIntent().getStringExtra("documentID");
.........
//when you update document use documentID
private void updateFirestoreEmotion() {
userIdRef.document(documentID)
.update("detected", detectedScoreText, "sentences", sentencesToneText)
.addOnSuccessListener(new OnSuccessListener<Void>() {
Upvotes: 1
Reputation: 317352
The error message is saying that you're trying to access with this document, with ID "0M0F1Ug5TmcO3pV":
main/0M0F1Ug5TmcO3pVaZu4XMluGOty1/journal/0M0F1Ug5TmcO3pV
But your screenshot is showing a different document that begins with "v91fz". The document from the error message simply doesn't exist, as the error message says.
The fact that your document has a field called "id" doesn't mean anything. The ID of the document as shown in the column with all the other document IDs is what matters for updates.
I think you've made a mistake with the user ID not being the same as the actual document ID. You use add() to create the document, which assign a random ID (not the user ID). Perhaps you want to actually use the user ID to create the document rather than a random ID. It's just not immediately clear what you intended, but the error message is correct.
Upvotes: 1