Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
225 views
in Technique[技术] by (71.8m points)

Firebase/Firestore: Value of type 'DocumentReference' has no member 'get'

I am trying to access a document and get value from the fields so I can login as a user and retrieve that user data. I have the following code that will not compile because I keep getting the error 'Value of type 'DocumentReference' has no member 'get'. Please help!

enter image description here

question from:https://stackoverflow.com/questions/65929339/firebase-firestore-value-of-type-documentreference-has-no-member-get

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your newDoc is a DocumentReference object, which is nothing more than a reference to a (potential) document in the database.

To load the document from the database you need to call getdocument() on the reference, as shown in this snippet from the documentation:

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

The data is only available within the completion handler, as shown above. Trying to access it outside of there will lead to timing problems, as the data is loaded asynchronously.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...