使用 firebase 函数将时间戳发送到 firebase

Send timestamp to firebase using firebase funtions

我需要使用 Firebase 函数将具有 firebase 时间戳对象的日期保存到 Cloud Firestore。

有问题的代码:

import FirebaseFunctions

lazy var functions = Functions.functions(region: "europe-west3")

let stamp = Timestamp(date: Date()) —> Not working

self.functions.httpsCallable("userUpdate").call(["id": user.uid, “startDate”: stamp]) { (result, error) in
                if let error = error {
                        print("##### update error \(error)”)
                    }
                }
                
                if let res = result {
                    if let fireData = (res.data as? [String: Any]) {
                        debugPrint("#####  Success update Data -> \(fireData["data"])")
                    }
                }
            }

问题是 Firebase 无法识别时间戳格式并抛出错误:

'Unsupported type: FIRTimestamp for value <FIRTimestamp: seconds=1633262425 nanoseconds=843380928>'

如果我尝试发送以下格式的日期,它可以工作,但服务器上的日期格式错误,它显示有一个字符串,但没有时间戳。

let date = DateFormatter.localizedString(from: Date(), dateStyle: .short, timeStyle: .none)

任何让 Firebase 识别我发送的日期的解决方案都有时间戳?

Callable Cloud Functions 只知道如何 send/receive JSON 类型,而 Timestamp 不是 JSON 类型。

解决此问题的最简单方法是从 Cloud Functions 代码中的那些 numberzs 发送 seconds and nanoseconds as separate numbers (which are valid JSON types) and then reconstruct a Timestamp object