【问题标题】:Firestore read documentFirestore 读取文档
【发布时间】:2020-09-22 13:29:57
【问题描述】:

我正在尝试从 Firestore 读取文档,但遇到了这个问题

Error Domain=FIRFirestoreErrorDomain Code=14 "获取文档失败,因为客户端离线。" UserInfo={NSLocalizedDescription=获取文档失败,因为客户端离线。}

注意:集合和文​​档是存在的 我使用的是 Firebase/Firestore 版本 1.17.1

谁能帮帮我?!

【问题讨论】:

  • 这个answer可能会帮助你
  • @Ashish 它对我不起作用
  • 错误信息提示您的设备没有互联网连接。

标签: swift firebase google-cloud-firestore


【解决方案1】:

这是使用 Cloud Firestore 获取数据的示例(来源:firebase.google.com)

首先创建数据:

let citiesRef = db.collection("cities")

citiesRef.document("SF").setData([
    "name": "San Francisco",
    "state": "CA",
    "country": "USA",
    "capital": false,
    "population": 860000,
    "regions": ["west_coast", "norcal"]
    ])
citiesRef.document("LA").setData([
    "name": "Los Angeles",
    "state": "CA",
    "country": "USA",
    "capital": false,
    "population": 3900000,
    "regions": ["west_coast", "socal"]
    ])
citiesRef.document("DC").setData([
    "name": "Washington D.C.",
    "country": "USA",
    "capital": true,
    "population": 680000,
    "regions": ["east_coast"]
    ])
citiesRef.document("TOK").setData([
    "name": "Tokyo",
    "country": "Japan",
    "capital": true,
    "population": 9000000,
    "regions": ["kanto", "honshu"]
    ])
citiesRef.document("BJ").setData([
    "name": "Beijing",
    "country": "China",
    "capital": true,
    "population": 21500000,
    "regions": ["jingjinji", "hebei"]
    ])

那么下面的例子展示了如何使用get()检索单个文档的内容:

let docRef = db.collection("cities").document("SF")

docRef.getDocument { (document, error) in
    if let document = document, document.exists {
        let dataDescription = document.data().map(String.init(describing:)) ?? "nil"
        print("Document data: \(dataDescription)")
    } else {
        print("Document does not exist")
    }
}

如果您使用firebase.firestore(),这也可能是一个错误,而firebase.database() 运行良好。 ?

【讨论】:

    猜你喜欢
    • 2021-09-25
    • 2021-11-18
    • 2019-10-06
    • 1970-01-01
    • 2020-11-03
    • 2020-12-17
    • 1970-01-01
    • 2020-02-24
    • 1970-01-01
    相关资源
    最近更新 更多