【问题标题】:get all collection field at once firestore一次获取所有收集字段
【发布时间】:2021-04-22 20:37:44
【问题描述】:

嗨,我在我的应用程序中使用了 firestore,一切正常,我可以获取文档,但我想要的是获取此文档中的字段。在应用程序中,它给了我像这样image1 的documentID,我想像这样image 2 显示这个文档中的字段

我试过这段代码,但它对我不起作用

import UIKit
import FirebaseFirestore
import Firebase

class orderTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {


@IBOutlet var order: UITableView!


var firstName = [String]()

   var db: Firestore!
 
override func viewDidLoad() {

    
    super.viewDidLoad()
    order.dataSource = self
    order.delegate = self
    
    db = Firestore.firestore()
    
  
    loadData()
  
}



func loadData() {
   
   
    db.collection("order").whereField(“firstname”, isEqualTo: true).getDocuments()  {(querySnapshot, err) in
                if let err = err {
                    print("Error getting documents: \(err)")
                } else {
                    for document in querySnapshot!.documents {

                        self.firstName.append(document.get("firstname") as? String ?? "")
                      
                    }
                }
                print(self.firstName)

                self.order.reloadData()
            }

        
        }
    



func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}


func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    240
}





func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {


return firstName.count
    
         }
    





func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    let cell = tableView.dequeueReusableCell(withIdentifier: "order", for: indexPath) as! orderTableViewCell
    
   
    let firstname = firstName[indexPath.row]
  
           print("Array is populated \(firstName)")
     
           return cell
 
}

}

【问题讨论】:

  • 它不起作用,因为您的数据库中的字段不是 firstname 此处使用 .whereField(“firstname” 和此处 .get("firstname")。根据截图是first name。但是你的语法是正确的let fName = doc.get("first name") as? String ?? "No First Name"。问题的其余部分尚不清楚,因为查询适用于所有真实的名字,并且您的屏幕截图显示名字字段是一个字符串。您还将获得与该查询匹配的所有文档,因此您可以为所有这些用户获得它。你能澄清一下这个问题吗?
  • @Jay 我想要的是我想要得到名字的值
  • @Jay 其实我想从每个文档中获取子集合
  • @Jay 请帮我解决这个问题,我从 1 个月开始就试图完成它,我在 stackOverFlow 中问了 3 次这个问题,但没有人回应
  • 对于真正有帮助的人,您的问题需要明确 1)您的文档没有问题中显示的子集合。 2)问题中的代码有错误 3)问题中的代码与链接的屏幕截图不匹配。所有这些都使问题变得非常不清楚。此外,链接可能会中断,如果发生这种情况,这个问题对未来的读者来说毫无用处,因为他们看不到您的 Firestore 结构。在操作问题中包含屏幕截图或结构并删除链接。

标签: arrays swift firebase google-cloud-firestore


【解决方案1】:

您需要使用 DocumentID 来获取字段值:

func loadData() {
    let db = Firestore.firestore()
    let ref = db.collection("users").document(documentID)
    ref.getDocument { (document, error) in
        if let document = document, document.exists {
            let dataDescription = document.data()!["first name"]
            self.firstName.append(dataDescription as! String)
        }
    }
}

这确实意味着您需要知道 documentID 是什么。创建文档时,您应该将其设置为唯一的用户 ID,并在用户会话期间将其保存在某处以访问它。

【讨论】:

  • 是的,我做到了,它适用于我,但可以获得每个 documentID 的字段?
  • @Faisal let dataDescription = document.data().map(String.init(describing:)) ?? "nil" 这会将完整的 JSON 作为字符串返回,例如:"["first name": "val"]"。您可以从此字符串中获取字段。
  • 非常感谢它对我有用,但可以在不使用特定文档 ID 的情况下做到这一点,或者这是不可能的?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-04
  • 1970-01-01
相关资源
最近更新 更多