Amoora Amassi
Amoora Amassi

Reputation: 9

Firestore read document

I am trying to read a document from Firestore but I face this problem

Error Domain=FIRFirestoreErrorDomain Code=14 "Failed to get document because the client is offline." UserInfo={NSLocalizedDescription=Failed to get document because the client is offline.}

Note : the collection and the document are existing I am use Firebase/Firestore version 1.17.1

could anyone help me please?!

Upvotes: 0

Views: 426

Answers (1)

user14321428
user14321428

Reputation:

This is an example of getting data with Cloud Firestore (source: firebase.google.com)

Firstly create data:

let citiesRef = db.collection("cities")

citiesRef.document("SF").setData([
    "name": "San Francisco",
    "state": "CA",
    "country": "USA",
    "capital": false,
    "population": 860000,
    "regions": ["west_coast", "norcal"]
    ])
citiesRef.document("LA").setData([
    "name": "Los Angeles",
    "state": "CA",
    "country": "USA",
    "capital": false,
    "population": 3900000,
    "regions": ["west_coast", "socal"]
    ])
citiesRef.document("DC").setData([
    "name": "Washington D.C.",
    "country": "USA",
    "capital": true,
    "population": 680000,
    "regions": ["east_coast"]
    ])
citiesRef.document("TOK").setData([
    "name": "Tokyo",
    "country": "Japan",
    "capital": true,
    "population": 9000000,
    "regions": ["kanto", "honshu"]
    ])
citiesRef.document("BJ").setData([
    "name": "Beijing",
    "country": "China",
    "capital": true,
    "population": 21500000,
    "regions": ["jingjinji", "hebei"]
    ])

Then the following example shows how to retrieve the contents of a single document using get():

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

This may be as well be an error if you are using firebase.firestore(), with firebase.database() it is working well. 😘

Upvotes: 0

Related Questions