0

I successfully get a document from Firestore.

The next question is how I should change the code, so that i get the latest Document that was added to the Collection.

I know that I can use some arguments like .order(by: String) or .limit(to: Int), but I don't know WHERE and HOW to use it.

I also looked after an answer in the Firestore Documentation, but there is just said how and what function you have to use for some specific examples.

I added the function, that should get this unknown document and a picture of how the Database Model looks like.

Code snippet:

func getSingleproperty() {

  var desiredProperty: String!
    
    let docRef = db.collection("UnKnownErrorMessages").document()


    docRef.getDocument { (document, error) in
      if let document = document, document.exists {
        let dataDescription = document.data().map(String.init(describing:)) ?? "nil"
        //Print all data in the document
        print("Document data: \(dataDescription)")

        if let allPropertiesInDocument = document.data() {

          let nameOfPropertyIwantToRetrieve = "read"

          if let selectedProperty = allPropertiesInDocument[nameOfPropertyIwantToRetrieve] {
            desiredProperty = selectedProperty as? String
          }
        }
        //Print exact the data that is in 'nameOfPropertyIwantToRetrieve' specified
        print("Value of desiredProperty is \(desiredProperty.description)")

      } else {
        print("Document does not exist \(error.debugDescription)")
      }
  }
}

Firestore database model (the variable inside the red box, is the variable that i want order by)

rocketFox
  • 77
  • 1
  • 8
  • https://firebase.google.com/docs/firestore/query-data/get-data?authuser=0#swift – trndjc Aug 27 '20 at 15:07
  • hey @tajh, i know that this documentation exists (looked up many times) but i don't know how to request this specific term. I must be doing an mistake and don't notice him. But thx anyway could have been possible that i didn't visited the documentation. – rocketFox Aug 27 '20 at 15:48

2 Answers2

1

You cannot read individual keys from a Firestore document. You always get the entire document.

To access the latest document, see https://stackoverflow.com/a/54178993

roshnet
  • 1,695
  • 18
  • 22
  • Okey, first thanks for this clarification, didn't new that i have to get the whole document. I tried to match the function to swift , but then i realised that doesn't work because the functions were made for angular not for swift. Sry if my question seems stupid to others but i don't get it how to get this done – rocketFox Aug 27 '20 at 16:25
1

Here is a real-world example of how you can retrieve a value from your Firestore database. In this case I've customized a query to your needs using the code provided in the Firestore documentation on this page, under "Add Data".

Please try the following:

  func getSingleproperty() {
    var desiredProperty: String!
    let docRef = db.collection("UnKnownErrorMessages").document("Error1234")

    docRef.getDocument { (document, error) in
        if let document = document, document.exists {
            let dataDescription = document.data().map(String.init(describing:)) ?? "nil"
            print("Document data: \(dataDescription)")
          
          if let allPropertiesInDocument = document.data() {
            
            let nameOfPropertyIwantToRetrieve = "errorMessage"
            
            if let selectedProperty = allPropertiesInDocument[nameOfPropertyIwantToRetrieve] {
              desiredProperty = selectedProperty as? String
            }
          }
          print("Value of desiredProperty is \(desiredProperty)")
          
        } else {
            print("Document does not exist")
        }
    }
  }
lukemmtt
  • 476
  • 6
  • 15
  • okey thx for your work, i appreciate your work. I tried it and an error occurred https://postimg.cc/kB60LH2x – rocketFox Aug 27 '20 at 17:53
  • That is a "warning", not an "error". Errors will stop compilation; warnings do not. https://learn.co/lessons/swift-xcode-warnings-errors. Please be sure to upvote and "accept" an answer if this helped you! – lukemmtt Aug 27 '20 at 17:59
  • Yeah i know :D but look in the console it says, that the document doesn't exist. But it does – rocketFox Aug 27 '20 at 18:00
  • Ah, I missed the debug console message, sorry. It seems you have to modify your Firestore security rules. That is beyond the scope of this question—please ask another question on the site if you need to resolve that issue. I tested the code in my app and it works properly. – lukemmtt Aug 27 '20 at 18:02
  • 1
    Okey, so thanks anyway. Helped a lot! – rocketFox Aug 27 '20 at 18:03
  • No problem, good luck! – lukemmtt Aug 27 '20 at 18:03
  • 1
    So, i looked up the security settings, which users are allowed to read/write from/to Firestore, and yeah. It was in safe mode. So nobody was allowed to make requests XD – rocketFox Aug 27 '20 at 18:31
  • One last mini question. How do i have to modify the query to get the latest Document that was added to the UnKnownErrorMessages Collection? I can't use where/don't know where to set it – rocketFox Aug 27 '20 at 19:02
  • Search is your friend: https://stackoverflow.com/questions/54178951/get-last-created-document-in-a-firebase-firestore-collection Alternatively you could start from a query like this: https://firebase.google.com/docs/firestore/query-data/get-data?authuser=0#get_multiple_documents_from_a_collection – lukemmtt Aug 28 '20 at 13:15
  • Oke, thanks, looks like have overseen the code in the documentation <3 it works now – rocketFox Aug 28 '20 at 14:01