简体   繁体   English

不支持解码(“DocumentID 值只能使用 Firestore.Decoder 解码”)

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

I have the following struct :我有以下struct

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

And this is how I'm parsing the data from Firestore:这就是我从 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") .正在调用catch语句,我收到以下错误消息: 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 .我研究过的所有文档都指向我使用JSONDecoder()来解析数据,但我在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.问题是我试图从没有id属性的源解码id Excluding id from my CodingKeys resolved the issue.从我的CodingKeys中排除id解决了这个问题。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Swift 中没有 documentID 的情况下在 firestore 中获取子集合? - How to fetch subcollections in firestore without documentID in Swift? 在 Swift 中使用 segue 传递 Firestore 的 documentID - Passing documentID of Firestore using segue in Swift iOS 上的 H.265 (HEVC) 解码 - 在可以解码帧之前正确的 NALU 顺序是什么 - H.265 (HEVC) Decoding on iOS - what is the correct NALU order before frames can be decoded 如何从 Firestore 检索自动生成的 documentID,以重用 documentID,因此我的数据不会被替换 - How to retrieve auto generated documentID from Firestore, to reuse the documentID, so my data isn't replaced 如何获取 swift iOS Firestore 中的单元格文档 ID? - How to get the post cell documentId in swift iOS Firestore? 使用 Codable 和 Firestore 进行非对称编码/解码? - Asymmetric Encoding/Decoding with Codable and Firestore? Swift 自定义解码包括另一个自定义解码 model - Swift custom decoding including another custom decoded model 这个解码器只会解码采用 NSSecureCoding 的类 - This decoder will only decode classes that adopt NSSecureCoding 我可以使用 Firestore 快照侦听器仅侦听缓存中的更改吗? - Can I use a Firestore snapshot listener to listen to changes only in cache? iOS Firestore 可以将`FieldValue.increment` 用于 Map 值吗? - iOS Firestore Can `FieldValue.increment` be used for Map values?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM