I need to save a date has a firebase Timestamp Object to Cloud Firestore using Firebase functions.
The code in question:
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"])")
}
}
}
The problem is that Firebase does not recognise the Timestamp format throwing a error:
'Unsupported type: FIRTimestamp for value <FIRTimestamp: seconds=1633262425 nanoseconds=843380928>'
If I try to send the date on the format below it works BUT the date on the server has the wrong format, it shows has a string and not has a Timestamp.
let date = DateFormatter.localizedString(from: Date(), dateStyle: .short, timeStyle: .none)
Any solutions for Firebase to recognise the date I'm sending has a Timestamp?
CodePudding user response:
Callable Cloud Functions only know how to send/receive JSON type, and Timestamp
is not a JSON type.
The easiest way to work around this is to send the seconds and nanoseconds as separate numbers (which are valid JSON types) and then reconstruct a Timestamp
object from those numberzs in the Cloud Functions code.