未处理的拒绝(FirebaseError):没有要更新的文档

Tho*_*sen 7 node.js firebase google-cloud-firestore

我对编码还很陌生,所以请耐心等待!我已经按照 YouTube 课程构建了一个笔记应用程序并获得了一个工作基础,但现在在删除 firebase 中的笔记时随机出现此错误,希望有人能够发现这里正在做什么!

“未处理的拒绝(FirebaseError):没有要更新的文档:projects/speakle-dc94b/databases/(默认)/documents/notes/GdWPrQNxR3Z9TFMWmqOZ”

它引用节点模块,如下所示: chrome 中错误的屏幕截图

我与 firebase 交互的代码如下所示:

componentDidMount = () => {
    firebase
      .firestore()
      .collection('notes')
      .onSnapshot(serverUpdate => {
        const notes = serverUpdate.docs.map(_doc => {
          const data = _doc.data();
          data['id'] = _doc.id;
          return data;
        });
        console.log(notes);
        this.setState({ notes: notes });
      });
  }

  selectNote = (note, index) => this.setState({ selectedNoteIndex: index, selectedNote: note });
  
  noteUpdate = (id, noteObj) => {
    firebase
      .firestore()
      .collection('notes')
      .doc(id)
      .update({
        title: noteObj.title,
        body: noteObj.body,
        timestamp: firebase.firestore.FieldValue.serverTimestamp()
      });
  }
  
  newNote = async (title) => {
    const note = {
      title: title,
      body: ''
    };
    const newFromDB = await firebase 
      .firestore()
      .collection('notes')  
      .add({
        title: note.title,
        body: note.body,
        timestamp: firebase.firestore.FieldValue.serverTimestamp()
      });
    const newID = newFromDB.id;
    await this.setState({ notes: [...this.state.notes, note] });
    const newNoteIndex = this.state.notes.indexOf(this.state.notes.filter(_note => _note.id === newID)[0]);
    this.setState({ selectedNote: this.state.notes[newNoteIndex], selectedNoteIndex: newNoteIndex });
  }

  deleteNote = async (note) => {
    const noteIndex = this.state.notes.indexOf(note);
    await this.setState({ notes: this.state.notes.filter(_note => _note !== note) })
    if(this.state.selectedNoteIndex === noteIndex) {
      this.setState({ selectedNoteIndex: null, selectedNote: null});
    } else {
      this.state.notes.lenght > 1 ? 
      this.selectNote(this.state.notes[this.state.selectedNoteIndex - 1], this.state.selectedNoteIndex - 1) : 
      this.setState({ selectedNoteIndex: null, selectedNote: null });
    }

    firebase 
      .firestore()
      .collection('notes')
      .doc(note.id)
      .delete()
      .then(function() {
        console.log("Document successfully deleted!");
    }).catch(function(error) {
        console.error("Error removing document: ", error);
    });
  }
}
Run Code Online (Sandbox Code Playgroud)

Sim*_*rla 3

这仅意味着没有要上传的同名文档。您可以使用set()add()添加该文档,因为它不存在。

noteUpdate = (id, noteObj) => {
    firebase
      .firestore()
      .collection('notes')
      .doc(id)
      .update({
        title: noteObj.title,
        body: noteObj.body,
        timestamp: firebase.firestore.FieldValue.serverTimestamp()
      });
  }
Run Code Online (Sandbox Code Playgroud)

用这个替换上面的代码

noteUpdate = (id, noteObj) => {
    firebase
      .firestore()
      .collection('notes')
      .doc(id)
      .add({
        title: noteObj.title,
        body: noteObj.body,
        timestamp: firebase.firestore.FieldValue.serverTimestamp()
      });
  } 
Run Code Online (Sandbox Code Playgroud)

或者

 noteUpdate = (id, noteObj) => {
    firebase
      .firestore()
      .collection('notes')
      .doc(id)
      .set({
        title: noteObj.title,
        body: noteObj.body,
        timestamp: firebase.firestore.FieldValue.serverTimestamp()
      });
  }

Run Code Online (Sandbox Code Playgroud)