Android:Firestore更新错误(未找到:无需更新的文档)

Android:Firestore更新错误(未找到:无需更新的文档),android,firebase,google-cloud-firestore,Android,Firebase,Google Cloud Firestore,我正试图更新firestore文档中的两个字段,但显示了一个错误,我不确定当该文档中有数据时,为什么错误声明找不到文档。数据是从上一个活动传递的。该文档已在上一个活动中添加到数据库中。 如何解决这个问题?提前谢谢你 E/Failed at update: NOT_FOUND: No document to update: projects/secretgarden-9defe/databases/(default)/documents/main/0M0F1Ug5TmcO3pVaZu4XMluGO

我正试图更新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());
            }
        });
    }

}


错误消息表示您正试图使用ID为“0M0F1Ug5TmcO3pV”的文档进行访问:

但您的屏幕截图显示的是另一个以“v91fz”开头的文档。正如错误消息所说,错误消息中的文档根本不存在

文档中有一个名为“id”的字段这一事实并不意味着什么。列中显示的文档ID和所有其他文档ID是更新的关键


我认为您犯了一个错误,用户ID与实际文档ID不一样。您使用add()创建文档,它分配一个随机ID(而不是用户ID)。也许您希望实际使用用户ID而不是随机ID来创建文档。只是不清楚您的意图,但错误消息是正确的。

错误消息表示您正试图使用ID为“0M0F1Ug5TmcO3pV”的文档进行访问:

但您的屏幕截图显示的是另一个以“v91fz”开头的文档。正如错误消息所说,错误消息中的文档根本不存在

文档中有一个名为“id”的字段这一事实并不意味着什么。列中显示的文档ID和所有其他文档ID是更新的关键


我认为您犯了一个错误,用户ID与实际文档ID不一样。您使用add()创建文档,它分配一个随机ID(而不是用户ID)。也许您希望实际使用用户ID而不是随机ID来创建文档。只是不清楚您想要的是什么,但错误消息是正确的。

因为您正在传递一个对象:

intent.putExtra("journal", journal);
我希望您的
Journal
类实现
Serializable
,这意味着:

//your class must implement Serializable

class Journal implements Serializable{

..........

}
更新

您没有将正确的id放入文档字段
id

这样做:

......
......

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();
.......
......
//更新文档时,请使用documentID

private void updateFirestoreEmotion() {

    userIdRef.document(documentID)
            .update("detected", detectedScoreText, "sentences", sentencesToneText)
            .addOnSuccessListener(new OnSuccessListener<Void>() {
private void updateFirestoreEmotion(){
userIdRef.document(documentID)
.update(“检测到的”,检测到的文字,“句子”,句子)
.addOnSuccessListener(新的OnSuccessListener(){

因为您正在传递一个对象:

intent.putExtra("journal", journal);
我希望您的
Journal
类实现
Serializable
,这意味着:

//your class must implement Serializable

class Journal implements Serializable{

..........

}
更新

您没有将正确的id放入文档字段
id

这样做:

......
......

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();
.......
......
//更新文档时,请使用documentID

private void updateFirestoreEmotion() {

    userIdRef.document(documentID)
            .update("detected", detectedScoreText, "sentences", sentencesToneText)
            .addOnSuccessListener(new OnSuccessListener<Void>() {
private void updateFirestoreEmotion(){
userIdRef.document(documentID)
.update(“检测到的”,检测到的文字,“句子”,句子)
.addOnSuccessListener(新的OnSuccessListener(){

您好,谢谢您的评论!文档中的“id”字段表示用户id(0M0F1Ug5TmcO3pV),所有文档都有相同的字段和来自相同用户的相同用户id,并且此特定文档id以“v91fz”开头,每个文档id都是自动生成且不同的。我仍在考虑如何解决ITI,感谢您的评论!文档中的“id”字段表示用户id(0M0F1Ug5TmcO3pV),所有文档都有相同的字段,该字段具有来自同一用户的相同用户id,并且该特定文档id以“v91fz”开头,每个文档id都是自动生成且不同的。我仍在考虑如何解决它。谢谢,我确实在我的日志类中实现了Serializable,但它仍然显示如下错误:(粘贴你的
日志
类。检查更新并保留可序列化实现。谢谢,我确实在我的日志类中实现了可序列化,但仍然显示如下错误:(粘贴你的
日志
类。检查更新并保留可序列化实现。)。