未处理的拒绝(FirebaseError):无文档可更新

时间:2020-02-05 11:56:40

标签: node.js firebase google-cloud-firestore

我对编码还是很陌生,所以请多多包涵!我遵循了youtube课程来构建笔记应用程序并获得使用基础,但是现在在删除Firebase中的笔记时,我会在随机时间收到此错误,希望有人可以在这里发现烹饪的内容!

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

它像这样引用节点模块: screenshot of the error in 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);
    });
  }
}

2 个答案:

答案 0 :(得分:1)

我只在 Cloud Functions 中使用过这样的东西,在编写端点以执行某些操作时,我遇到了下面引用的错误。

我试图读取一个集合中的文档,如果它存在,我试图将一个新文档写入另一个集合。所以这是一种嵌套代码。

我的一段代码。

    const firstDocRef = db.collection('myFirstCollection').doc('myDocName');
    const existDoc = firstDocRef.get()
    .then((resDoc)=>{
        if(resDoc.exists)
        {
            db.collection('mySecondCollection').add({
                .
                .
                .
                .
                .
                orderCreatedAt:Firestore.FieldValue.serverTimestamp()
            })
            .then((new_doc)=>{
                return res.status(200);
                // return 200 ok what we were trying to achieve has completed.
            })
            .catch(()=>{
                console.log("Log, as write failed somehow");
                return res.status(500);
                // return a 500 internal server error occurred
            });
        }
        else
        {
            console.log("My first condition wasn't met, so logged it");
            return res.end();
            // properly terminate the processing of request
        }
    });
    /*.catch((err)=>{
        console.log("Our first doc presence check couldn't complete and hence I arrived here, log it");
        res.writeHead(500);
        return res.end();
        // again give back 500 to client
    });*/
<块引用>

UnhandledPromiseRejectionWarning: ReferenceError: Firestore 未定义 UnhandledPromiseRejectionWarning:未处理的承诺拒绝。 此错误源于在没有 catch 块的异步函数内部抛出

现在我也是 Firebase 的新手,但我遇到了这个问题并以某种方式解决了它。

因此,如果我在 get() 文档中放入 catch 块,我不会收到上述错误。 奇怪哈!

通过注释删除了 catch 块。收到此错误。

现在,这是一个失控的错误,它说渔获物不在那里,但我们是故意这样做的。

所以我开始搜索,在堆栈溢出时遇到了这个问题,发现它仍然没有答案。自己搜索并阅读了文档。

我想告诉您,这不是因为任何 Firestore 安全规则或其他任何原因。因为我在寻找答案时也对这些概念有一些猜测。

<块引用>

我们在这里所做的共同点是我们试图在 FireStore 上实现 ServerTimeStamp

我想在我的节点云函数代码中将您的通知带到我的导入中。

const functions = require('firebase-functions');
const express = require('express');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
const db = admin.firestore();

所以你看,我正在使用新的方式来获得使用 Firestore 的权限,因为我正在尝试建立一个云功能。

现在这是 Google 提供的文档参考:点击 here

上述 API 参考建议的正确语法是

Firestore.FieldValue.serverTimestamp()

它是罪魁祸首,它没有为我提供任何时间戳,如果没有 catch 块未处理的承诺错误发生并且调试时没有显示错误,它就不起作用。

我这样做了,解决方案部分:

即使在我的节点程序中导入这些内容之后,我还是导入了这个:

const {Firestore} = require('@google-cloud/firestore');

现在我所做的就是将时间戳字段中的语句用作

Firestore.FieldValue.serverTimestamp()

正如前面提到的,甚至使用了一个 catch 块,以防在生产过程中出现任何其他问题。那是使用 db 常量来完成所有数据库事务性的事情,而对于 serverTimeStamp,我必须引入新的导入。

它奏效了,我想 require('@google-cloud/firestore') 语句导入为 {FireStore} 带来了 FieldValue 事物用作参考所需的所有内容。

我希望它可以帮助任何寻找它的新人,并节省我浪费在寻找解决方案上的大量时间。

我已经通过在 firebase 模拟器上运行云功能进行了验证。

答案 1 :(得分:0)

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

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

用这个替换上面的代码

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

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