【问题标题】:(Swift)Tell me the reason why I cannot query the items(Swift)告诉我无法查询项目的原因
【发布时间】:2020-06-08 06:25:31
【问题描述】:
   var menuArray: [String] = []
      override func viewDidLoad() {
       super.viewDidLoad()
       fetchData {
           self.tableView.reloadData()
            print(self.menuArray)
       }
   }
      func fetchData(completion: @escaping () -> Void){

      let user = Auth.auth().currentUser
      let saveDocument = Firestore.firestore()
      let uploadDocument = saveDocument.collection("Posts")

      let query = uploadDocument.whereField("LikeId", isEqualTo: user!.uid)

        query.getDocuments { (querySnapshot, error) in

         if error != nil {
                print("error")
            } else {
                for document in querySnapshot!.documents {
                   if let menuValue = document.data()["Menu"] as? String{
                        self.menuArray.append(menuValue)
                        print(menuValue)
                        print(self.menuArray)

                    }}}}                  
            completion()

我不知道为什么会出现 print Items [ ](nil)

也许查询部分是错误的,...
我使用 whereField 来查找 User!.uid 我不知道这段代码错误的原因。 请告诉我这样做的原因。

【问题讨论】:

    标签: ios swift firebase google-cloud-firestore


    【解决方案1】:

    LikeId是一个数组,你需要做以下检查数组是否包含值:

     let query = uploadDocument.whereField("LikeId",  arrayContains: user!.uid)
    

    https://firebase.google.com/docs/firestore/query-data/queries#array_membership

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-17
      • 2022-11-24
      • 2020-09-06
      • 1970-01-01
      • 2017-07-22
      • 2023-03-16
      • 1970-01-01
      相关资源
      最近更新 更多