【问题标题】:Firebase/Firestore document not updatingFirebase/Firestore 文档未更新
【发布时间】:2020-01-08 11:18:25
【问题描述】:

我在使用 angular/firestore 和 Firebase 更新文档数据时遇到问题。

我尝试更新的值已在 DOM 中成功更新,但未保存在 Firebase 中,因此它会在页面重新加载时重置为原始值。

我收到此错误:

FirebaseError:函数 DocumentReference.update() 至少需要 2 个参数,但调用时使用了 1 个参数

为我服务:

updateWord(word: Words, newState: string) {
  this.firestore.doc("words/" + word.word).update(word.state = newState);
}

在我的 component.ts 中:

changeState(word: Words, state: string) {
  this.wordsService.updateWord(word, state);
}

【问题讨论】:

    标签: typescript firebase google-cloud-firestore


    【解决方案1】:

    update 方法需要一个 Partial<Words> 对象,你正在用一个字符串更新它。

    newState 的值是从word.state = newState 赋值返回的

    您应该只将包含新更新的对象传递给您的对象:

    updateWord(word: Words, newState: string) {
      this.firestore.doc<Words>(`words/`${word.word}).update({ state: newState });
    }
    

    阅读更多 here 了解 Firestore 的适当 API 用法

    不要忘记错误处理和/或从更新调用返回承诺,这样你就可以链接它并处理可能发生的任何错误

    【讨论】:

    • 谢谢,为这个错误工作!它现在给了我一个“没有要更新的文档”,但它似乎知道该文档......“word.word”正在给我所有的文档信息。
    • @LLP word.word 的内容是什么?您需要找到单词的 id 来更新正确的路径,我以为您将其存储在 word.word 中。
    • Id 是缺失的部分!它现在完美无缺。 Id 不在我的“Words”界面中,而是由 firebase 生成的,所以我没有使用它。
    猜你喜欢
    • 1970-01-01
    • 2018-09-15
    • 2021-09-02
    • 1970-01-01
    • 1970-01-01
    • 2021-02-05
    • 2019-02-06
    • 2023-02-07
    • 2020-07-27
    相关资源
    最近更新 更多