简体   繁体   中英

Decoding Is Not Supported ("DocumentID values can only be decoded with Firestore.Decoder")

I have the following struct :

struct Recipe: Codable {
    @DocumentID var id: String?
    var vegetarian: Bool?
}

And this is how I'm parsing the data from Firestore:

do {
                    let decoder = JSONDecoder()
                    let recipeToDisplay = try decoder.decode(Recipe.self, from: data!)
                    
                    let uuid = UUID().uuidString
                    
                    FirestoreService.createRecipe(
                        documentId:uuid,
                        vegetarian: recipeToDisplay.vegetarian ?? false
                    ) { recipeURL in
                        print("success")
                    }
                }
                catch {
                    print("Error parsing response data: \(error)")
                }

The catch statement is getting called and I'm getting the following error message: decodingIsNotSupported("DocumentID values can only be decoded with Firestore.Decoder") .

All the documentation I've researched has pointed me towards using JSONDecoder() to parse the data and I can't find anything on Firestore.Decoder . Is there a different way that I should be parsing the data?

The issue was that I was trying to decode id from a source that didn't have an id property. Excluding id from my CodingKeys resolved the issue.

struct Recipe: Codable {
    @DocumentID var id: String?
    var vegetarian: Bool?
    
    private enum CodingKeys: String, CodingKey {
        case id
        case vegetarian
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM