[英]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.