Firestore getDocument not working

661 views
Skip to first unread message

AB Dev

unread,
Oct 7, 2018, 11:02:48 PM10/7/18
to Firebase Google Group
I am unable to get Firestore getDocuments working

I am trying this
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”)
  }
}


I have updated the pods

docRef.getDocument doesnt return anything
The collection exists in the firebase and I can print the collection ID
I just cant iterate through the docs in the collection

Can anyone help me with this?


Thanks,
AB

Samuel Stern

unread,
Oct 9, 2018, 12:53:38 PM10/9/18
to fireba...@googlegroups.com
Hi AB,

In the code you showed above there is an "error" parameter passed into the callback.  Could you print out the error and then show me what it says?  That should help determine why you can't fetch the document. 

Thanks,
SAm

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/c74c71a1-8699-4626-aae7-bdc51a6f82e7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

AB Dev

unread,
Oct 9, 2018, 3:13:30 PM10/9/18
to Firebase Google Group
Hi Sam,

Thanks for having a look, the problem is that even that’s not being hit during debugging. While debugging that whole block starting from getDocs is skipped.
I am not sure if I have coded the completion block incorrectly. I expect the code from Firestore sample webpages to work but, I am unable to making reading from a document work.
Moreover, if I do get documentID on the doc then it works. But, I am unable to read the values inside the document.

Thank you,
Abhinav

Samuel Stern

unread,
Oct 9, 2018, 3:22:39 PM10/9/18
to fireba...@googlegroups.com
Hi Abhinav,

The completion in the getDocument call is asynchronous, so "skipping over" it when debugging is the expected behavior.  It will call back once the document is read, or if the read fails.  That should happen <1s later.  Do you see either of the print() messages in your logs?

- Sam

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.

AB Dev

unread,
Oct 9, 2018, 4:27:45 PM10/9/18
to Firebase Google Group
Hi Sam,

Oh ok. Though I have tried printing the error, I never saw anything in the logs. I will try again and let you know.

Thanks,
Abhinav

AB Dev

unread,
Oct 9, 2018, 8:17:52 PM10/9/18
to Firebase Google Group
Hi Sam,

I changed the block to this:

let db = Firestore.firestore()

        let docReff = db.collection("sampleData").document("Inspiration")

        

        docReff.getDocument { (document, error) in

            if let err = error {

                print("ERROR")

                print("Error getting documents: \(err)")

            }

            

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

            }

            

            print("here")

            print(error.debugDescription)

        }



And, nothing ever prints on the console.
I am not sure what I am doing wrong

Thanks,
AB

AB Dev

unread,
Oct 9, 2018, 11:52:44 PM10/9/18
to Firebase Google Group
Hi Sam,

Thanks for all your help!
Its working now, there was a problem with the rules in the Firebase project console.

Thanks again!
AB

On Tuesday, October 9, 2018 at 3:22:39 PM UTC-4, Samuel Stern wrote:

Samuel Stern

unread,
Oct 10, 2018, 11:43:28 AM10/10/18
to fireba...@googlegroups.com
Glad you got it working!  For future reference when you're having rules issues you should see your "error" block show a "Permission Denied" error.

- Sam

Reply all
Reply to author
Forward
0 new messages