我正在尝试更新Firestore文档中的两个字段,但是显示了错误,我不确定为什么该错误指出当文档中有数据时找不到文档。数据来自上一个活动。该文档已在上一个活动中添加到数据库中。 如何解决这个问题呢?谢谢你。
E/Failed at update: NOT_FOUND: No document to update: projects/secretgarden-9defe/databases/(default)/documents/main/0M0F1Ug5TmcO3pVaZu4XMluGOty1/journal/0M0F1Ug5TmcO3pV
//这是ReceivedActivity,它接收了先前活动传递的数据
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());
}
});
}
}
//这是Firestore中的结构(我只想使用ReceivedActivity中的数据更新“检测到的”和“句子”字段)
//以前的活动相关信息
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;
}
}
答案 0 :(得分:1)
错误消息表明您正在尝试使用ID为“ 0M0F1Ug5TmcO3pV”的此文档进行访问:
main/0M0F1Ug5TmcO3pVaZu4XMluGOty1/journal/0M0F1Ug5TmcO3pV
但是您的屏幕快照显示的是另一个以“ v91fz”开头的文档。正如错误消息所述,错误消息中的文档根本不存在。
您的文档具有名为“ id”的字段这一事实并不意味着任何事情。列中显示的文档ID和其他所有文档ID才是更新的重要内容。
我认为您犯了一个错误,因为用户ID与实际的文档ID不同。您可以使用add()创建文档,该文档分配一个随机ID(而不是用户ID)。也许您想实际使用用户ID而不是随机ID来创建文档。只是现在还不清楚您的意图,但是错误消息是正确的。
答案 1 :(得分:1)
由于您要传递对象:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:set="http://exslt.org/sets"
xmlns:saxon="http://icl.com/saxon"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl"
xmlns:d="http://docbook.org/ns/docbook"
exclude-result-prefixes="d"
version="1.0">
<?cco_xsltype addon="DocBook5.0" toolchain="DocBook_to_PDF_FOP"?>
<?cco_xsltype addon="DocBook5.0" toolchain="DocBook_to_PS_FOP"?>
<!-- Import standard -->
<xsl:import href="simpleXMLtoXML.xsl"/>
<xsl:import href="http://docbook.sourceforge.net/release/xsl/current/fo/profile-docbook.xsl"/>
<!-- DocBook XSL Parameters -->
<xsl:param select="1" name="fop1.extensions"/>
<xsl:param name="paper.type">A4</xsl:param>
<xsl:template match="book">
<xsl:variable name="content">
<xsl:apply-imports/>
</xsl:variable>
<xsl:apply-templates select="exsl:node-set($content)"/>
</xsl:template>
</xsl:stylesheet>
我希望您的autopep8 -i <filename>.py
类实现intent.putExtra("journal", journal);
,这意味着:
Journal
更新
您没有在文档字段Serializable
中输入正确的ID
执行此操作:
//your class must implement Serializable
class Journal implements Serializable{
..........
}
在下一个活动中,同时获取对象和documentID:
id
///当您更新文档时使用documentID
......
......
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();
.......
......