javascript - 在 Firestore 上添加数据
问题描述
我正在尝试将数据保存在 Firestore 数据库中
db.collection('clients').doc(clientInformation['id']).set(clientInformation)
.then( result => {
res.status(200).send({ client: result });
});
如果提供的 id 已经存在于 db 上,我想要得到一个错误,我知道 firestore 会覆盖它或为您提供合并此数据的选项,但它可能会使用不同于“设置”并抓住它?
谢谢!
解决方案
首先尝试获取您要保存的特定信息。它不存在,写它,否则更新它。
docRef.get().then(function(doc) {
if (doc.exists) {
console.log("Document already exists:", doc.data());
} else {
// doc.data() will be undefined in this case
console.log("No such document we can .set it");
}
}).catch(function(error) {
console.log("Error getting document:", error);
});
推荐阅读
- python - Django - 从父级查询许多 FK 模型
- flutter - 在颤动中自动在应用程序主屏幕加载上显示警报对话框
- java - 是否可以使用所有片段创建应用程序?
- python - python 两种形式的矩阵x[i,j]和x[i][j]的区别
- android - Android Microsoft Translator API-获取支持翻译文本的语言时出错
- vba - Excel:当最后一列更改编号时如何获取最后一列的列索引号
- jquery - 关闭引导程序 3 模态后滚动正文
- java - 如何在我的 Gluon Mobile 项目中更改 jdk?
- python - 使用子进程从 input() 交互式运行多个命令
- c# - 使用 XAML 样式的 WPF 3d 按钮样式