javascript - 如果文档已经存在,不想覆盖它
问题描述
这将覆盖它。
firebase.firestore().collection('test_collection').doc(this.id).set({
body: this.body,
}, { merge: true })
{merge: true} 无论是否存在都没有改变。
解决方案
{merge: true} 用于合并两个对象,但会覆盖 props。
您必须手动检查文档是否存在:
firebase.firestore().collection('test_collection')
.doc(this.id)
.get()
.then(docSnapshot => {
if(!docSnapshot.exists)
docSnapshot.ref.set({body: this.body})
})
推荐阅读
- visual-studio - C++ zbar 库 - 未解析的外部符号
- javascript - 使用纯javascript获取svg矩形的填充颜色
- reactjs - 递归无法正常工作(在 React 中)
- c# - 带有 IS4 的 Mac 上的 Dotnet 5 返回 SSL 连接无法建立
- serverless-framework - 如何在 serverless.ts 中为 json 模式设置文件名
- laravel - 使用 Laravel 关系函数时将属性标记为可填充
- java - 将 logback.xml 转换为 application.properties
- kotlin - 如何在 kotlin 中将“项目”对象添加到对象 ArrayList
- amazon-web-services - 仅授予对 Redshift Spectrum 中的 View 的访问权限
- java - 将 SonarLint 连接到 SonarQubeServer 但没有映射问题