【问题标题】:Cloud Firestore: How to get a specific value from all random generated documents [closed]Cloud Firestore:如何从所有随机生成的文档中获取特定值 [关闭]
【发布时间】:2020-09-07 17:26:24
【问题描述】:

有没有一种方法可以获取在 Firestore 中创建的所有文档的特定值?

这就是我的 Firestore 的结构:

firebase_screenshot

我想获取“纬度”和“经度”数据,以便在 mapView 上添加注释。因此,对于每个“帖子”,都会有一个注释。

Firebase 文档中提供的代码(我也在下面分享)似乎对我不起作用,因为我只能获取一个特定路径内所有元素的数据。 p>

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")
    }
}

问题是,即使我输入了那个特定的路径,我怎么能得到其他路径的“纬度”和“经度”值呢?我还有什么遗漏的吗?

我希望这是有道理的。我是编程新手,并尽力解释。我已经为此苦苦挣扎了两天了。

【问题讨论】:

    标签: ios swift firebase google-cloud-firestore annotations


    【解决方案1】:

    看起来您需要从文档中检索这些值,请尝试以下操作:

    let docRef = db.collection("cities").document("SF")
    docRef.getDocument { (document, error) in
       if let document = document, document.exists {
        let data = document.data()
        let latitude = data["latitude"] as? String, 
        let longitude =  data["longitude"] as? String{
        print("latitude = \(latitude)")
        print("longitude = \(longitude)")
    } else {
        print("Document does not exist")
    }
    

    您还可以为更简洁的实现创建特定的结构:

    let coordenates = coordenate(latitude: latitude, longitude: longitude)
    print("coordenates: \(coordenates)")
    

    【讨论】:

    • 您好 Alexis,此代码似乎不起作用,因为我收到以下错误:“使用未解析的标识符‘数据’”和“使用未解析的标识符‘文档’”
    • 在原始代码中粘贴它的内容以获得更好的上下文:)
    猜你喜欢
    • 2023-03-28
    • 2020-07-29
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多