-
-
Notifications
You must be signed in to change notification settings - Fork 112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bug(firestore): Timestamp values #474
Comments
In poking around the codebase, I've narrowed down the problem to the |
Thank you for reporting this issue! Would you be willing to create a PR with your idea? I would then take a closer look. |
Sure thing, @robingenz . I'll see if I can get that together, this morning. |
@robingenz Could you please provide any updates on this matter or offer an estimated timeframe for resolving the issue? Thank you. |
@SureshKumarToverto As already mentioned in #564 (comment), this is currently a bug in Capacitor (see ionic-team/capacitor#7252). I have to wait until it's fixed. |
@robingenz does this same bug also affect geopoints? we store both timestamps and geopoints in our firestore db, everything comes back fine when testing in the browser, but when we compile into a native iOS app and test on the device, neither timestamps/geopoints come back |
@johnmckay-reward No, this should be another issue. Please create a separate bug report. |
Is there a workaround for this bug while waiting for the capacitor team to fix ionic-team/capacitor#7252, or should I switch to storing my dates as strings as opposed to the Timestamp type in firestore to avoid this issue? |
No, there is currently no workaround. I have another idea on how to solve this, but of course I would prefer the problem to be solved in the Capacitor Core. I currently save all timestamps as ISO 8601 strings. |
Any update, this is pretty crucial I would think to any app using firestore? |
No, there are no updates yet. It's still on my to-do list for this year. For the time being, I recommend saving all timestamps as ISO 8601 strings. |
I'm still facing the same issue (on iOS). Are there any updates regarding this? |
Saving the dates as strings to Firestore is not ideal as you will lose querying functionality that is possible with timestamp fields. In the meantime I have made a local patch package to update the swift files to handle timestamps and output them as ISO 8601 strings from the plugin instead of null. Then it is easy to convert to a javascript date object from there. The helper struct I have defined in swift looks like this: import FirebaseFirestore
struct FirestoreHelper {
static func convertTimestampsInDocument(data: [String: Any]) -> [String: Any] {
var convertedData = [String: Any]()
for (key, value) in data {
if let timestamp = value as? Timestamp {
// Convert Timestamp to Date
let date = timestamp.dateValue()
// Use the Date extension to convert Date to ISO 8601 string
convertedData[key] = date.toISO8601String()
} else if let nestedData = value as? [String: Any] {
// Recursively convert nested dictionaries
convertedData[key] = convertTimestampsInDocument(data: nestedData)
} else {
convertedData[key] = value
}
}
return convertedData
}
}
extension Date {
func toISO8601String() -> String {
let formatter = ISO8601DateFormatter()
formatter.timeZone = TimeZone(secondsFromGMT: 0) // Set to UTC for consistency
return formatter.string(from: self)
}
} The I was able to modify each toJSObject() method for the Results classes like so: public func toJSObject() -> AnyObject {
// Convert the Firestore timestamps
let rawData = documentSnapshot.data() ?? [:]
let convertedData = FirestoreHelper.convertTimestampsInDocument(data: rawData)
let snapshotDataResult = FirebaseFirestoreHelper.createJSObjectFromHashMap(convertedData)
var snapshotResult = JSObject()
snapshotResult["id"] = documentSnapshot.documentID
snapshotResult["path"] = documentSnapshot.reference.path
if let snapshotDataResult = snapshotDataResult {
snapshotResult["data"] = snapshotDataResult
} else {
snapshotResult["data"] = NSNull()
}
var metadata = JSObject()
metadata["fromCache"] = documentSnapshot.metadata.isFromCache
metadata["hasPendingWrites"] = documentSnapshot.metadata.hasPendingWrites
snapshotResult["metadata"] = metadata
var result = JSObject()
result["snapshot"] = snapshotResult
return result as AnyObject
} Hope that helps as a temporary solution and maybe can assist towards a permanent solution too. |
This is actually a much more efficient place to put the conversion. I think I will adjust my implementation to match this. That way you only need to iterate over the fields in a document once instead of twice. |
Plugin(s)
Did you test the latest version?
Platform(s)
Current behavior
If I read a document using the
FirebaseFirestore.getDocument
method, itssnapshot.data
object is completely missing any value that resolves as a FirestoreTimestamp
value. For example, a document of the following format in Firebase ...... is returned as ...
Expected behavior
I would expect that any
snapshot.data
value has certain values in Firestore, it this method would return those values as part of its resolved object structure.Reproduction
https://github.com/Paganiniana/capawesome-bug-reproduction
Steps to reproduce
(See the readme)
Other information
No response
Capacitor doctor
💊 Capacitor Doctor 💊
Latest Dependencies:
@capacitor/cli: 5.5.0
@capacitor/core: 5.5.0
@capacitor/android: 5.5.0
@capacitor/ios: 5.5.0
Installed Dependencies:
@capacitor/android: not installed
@capacitor/cli: 5.5.0
@capacitor/ios: 5.5.0
@capacitor/core: 5.5.0
[success] iOS looking great! 👌
Before submitting
The text was updated successfully, but these errors were encountered: