I'm working with firebase and I have a function which returns a promise from a document reference.
func getCatalogItem(ref: DocumentReference) -> Promise<Catalog> {
return Promise { seal in
ref.getDocument() { result, err in
if err != nil {
seal.reject(err!)
} else {
let data = FirestoreDecoder()
do {
let item = try data.decode(Catalog.self, from: result!.data()!)
seal.fulfill(item)
} catch {
print("Error")
}
}
}
}
}
It works fine but the only thing is the return type is a custom type which I decoded from the document reference call but the call itself is synchronous and it crashes my app with a nil error because the data isn't actually there yet.
Here's the line and function that produces the error (nil)
let item = try data.decode(Catalog.self, from: result!.data()!)
I tried making another function with a handler for the firebase function and passing that value to the decoder but that also returned nil, and (I'm using awaitkit/promisekit) I tried messing around with async/await but couldn't get that to work. How could I fix this?
Screenshot of the document in my database:

and then i have this:
let featureditem = Firestore.firestore().collection("catalog").document("TDS faded jeans")
self.featureditem.getDocument() { doc, err in
if doc == doc {
let item = try! FirestoreDecoder().decode(Catalog.self, from: doc!.data()!)
print("\(item)")
Which is also giving me nil.
!). Especially avoid using multiple times on one line, making it incredibly hard to figure out what failed. So, unwrapresultfirst (e.g.guard let result = result else { print("result was nil"); seal.reject(...); return }. Then use the same pattern to unwrap whateverdata()returned. Then look at what your decoder returned (which I’d rename fromdatatodecoderto avoid confusion), if it got that far.rejectin yourcatchstatement, e.g.,seal.reject(error). And if you’re going toprintanything, I’d probably print the actual error, not just the string literal"Error".