【问题标题】:How to check if firestore database document exist when given a document id?给定文档ID时如何检查firestore数据库文档是否存在?
【发布时间】:2020-04-24 18:33:33
【问题描述】:

我想在给定文档 ID 时检查 firestore 中的文档是否存在。到目前为止,我已经尝试过:

String getUserType(String uid) {
final result = Firestore.instance.collection('patients').document(uid).get();
if (result == null) {
  return 'null';
} else {
  return 'exist';
}    

【问题讨论】:

标签: firebase flutter google-cloud-firestore


【解决方案1】:

你可以使用result.exists

原帖:https://stackoverflow.com/a/56465899/4465386

final result = await Firestore.instance
  .collection('posts')
  .document(docId)
  .get()

if (result == null || !result.exists) {
  // Document with id == docId doesn't exist.
}

【讨论】:

  • 为什么它显示我的文档存在,即使它不存在?
  • 该文档是否在您无法从 UI 中看到的集合中?如果是,则从 UI 手动创建该集合并添加一个随机文档。有时,除非您手动创建集合,否则 UI 不会向您显示集合及其内容。
猜你喜欢
  • 2019-01-09
  • 2021-07-22
  • 2019-09-24
  • 1970-01-01
  • 1970-01-01
  • 2019-02-19
  • 2020-06-08
  • 2021-11-25
相关资源
最近更新 更多