简体   繁体   English

将Codable Struct上传到Swift中的Firebase数据库

[英]Upload Codable Struct to Firebase Database in Swift

I am trying to upload some data into my Firebase Realtime Database but got this error while trying to upload the data: *** Terminating app due to uncaught exception 'InvalidFirebaseData', reason: '(updateChildValues:) Cannot store object of type __SwiftValue at. Can only store objects of type NSNumber, NSString, NSDictionary, and NSArray.'我正在尝试将一些数据上传到我的 Firebase 实时数据库,但在尝试上传数据时出现此错误: *** Terminating app due to uncaught exception 'InvalidFirebaseData', reason: '(updateChildValues:) Cannot store object of type __SwiftValue at. Can only store objects of type NSNumber, NSString, NSDictionary, and NSArray.' *** Terminating app due to uncaught exception 'InvalidFirebaseData', reason: '(updateChildValues:) Cannot store object of type __SwiftValue at. Can only store objects of type NSNumber, NSString, NSDictionary, and NSArray.'

Is there a way to easily upload my codable struct to my DB?有没有办法轻松地将我的可编码结构上传到我的数据库?

func uploadMerchOrder(orderNo: String, email: String?, totalAmount: Int, usingCard: Bool, lineItems: [PrintifyLineItems], address: PrintifyAddress) {
    guard let user = AuthService.shared.user else { return }
    
    let values: [String: Any] = [
        "userID": user.id,
        "email": email ?? user.email,
        "orderID": orderNo,
        "totalAmount": totalAmount,
        "usingCard": usingCard,
        "items": lineItems,
        "address": address
    ]
    
    merchOrders.child(orderNo).updateChildValues(values)
}

It looks like your lineItems and address values are not of a type that can be stored in the Firebase Realtime Database.看起来您的 lineItems 和地址值不是可以存储在 Firebase 实时数据库中的类型。 The Firebase Realtime Database can only store values that are one of the following types: NSNumber, NSString, NSDictionary, or NSArray. Firebase 实时数据库只能存储以下类型之一的值:NSNumber、NSString、NSDictionary 或 NSArray。

You can fix this issue by encoding your lineItems and address values as dictionaries before passing them to the updateChildValues(_:) method.您可以通过在将 lineItems 和地址值传递给 updateChildValues(_:) 方法之前将它们编码为字典来解决此问题。 You can use the JSONEncoder class to encode your values as dictionaries, like this:您可以使用 JSONEncoder class 将您的值编码为字典,如下所示:

let jsonEncoder = JSONEncoder()

let lineItemsData = try jsonEncoder.encode(lineItems)
let lineItemsDict = try JSONSerialization.jsonObject(with: lineItemsData, options: []) as? [String: Any]

let addressData = try jsonEncoder.encode(address)
let addressDict = try JSONSerialization.jsonObject(with: addressData, options: []) as? [String: Any]

let values: [String: Any] = [
    "userID": user.id,
    "email": email ?? user.email,
    "orderID": orderNo,
    "totalAmount": totalAmount,
    "usingCard": usingCard,
    "items": lineItemsDict,
    "address": addressDict
]

merchOrders.child(orderNo).updateChildValues(values)

This will encode your lineItems and address values as dictionaries and then pass them to the updateChildValues(_:) method, which should fix the error you are seeing.这会将您的 lineItems 和地址值编码为字典,然后将它们传递给 updateChildValues(_:) 方法,这应该可以修复您看到的错误。

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

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