Firebase / Firestore文档未更新

时间:2020-01-08 11:18:25

标签: typescript firebase google-cloud-firestore

我无法使用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);
}

1 个答案:

答案 0 :(得分:2)

更新方法需要一个Partial<Words>对象,您正在使用字符串对其进行更新。

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

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

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

了解更多here,了解适当的firestore API用法

别忘了处理错误和/或从更新调用返回承诺,因此您可以将其链接起来并处理可能发生的任何错误