Skip to content
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

🐛 [cloud_firestore] Getting LegacyJavaScriptObject error with Timestamps #12249

Closed
AVancans opened this issue Feb 1, 2024 · 8 comments · Fixed by #12272
Closed

🐛 [cloud_firestore] Getting LegacyJavaScriptObject error with Timestamps #12249

AVancans opened this issue Feb 1, 2024 · 8 comments · Fixed by #12272
Assignees
Labels
resolution: fixed A fix has been merged or is pending merge from a PR. type: bug Something isn't working

Comments

@AVancans
Copy link

AVancans commented Feb 1, 2024

[Platform: Web]

Unable to decode/infer Timestamp objects, throws Error: Expected a value of type 'Timestamp', but got one of type 'LegacyJavaScriptObject' even though when printed, the map shows Timestamp(seconds=1703827286, nanoseconds=951000000) but createdTimestamp.runtimeType prints LegacyJavaScriptObject

class Session {
 Timestamp createdTimestamp;

 static Session of(Map<String, dynamic> map) {
    return Session(map["createdTimestamp"]);
 }
}

Flutter dependencies

  firebase_core: ^2.25.1
  cloud_firestore: ^4.15.0
  firebase_auth: ^4.17.0

Rendering: canvaskit

@AVancans AVancans added Needs Attention This issue needs maintainer attention. type: bug Something isn't working labels Feb 1, 2024
@darshankawar darshankawar added the triage Issue is currently being triaged. label Feb 1, 2024
@darshankawar
Copy link

@AVancans
Maybe you are hitting a related issue as #12237 that you can check.
Meanwhile, I was unable to replicate using the code snippet you provided. Can you provide runnable code sample ?

@darshankawar darshankawar added blocked: customer-response Waiting for customer response, e.g. more information was requested. and removed Needs Attention This issue needs maintainer attention. labels Feb 1, 2024
@AVancans
Copy link
Author

AVancans commented Feb 1, 2024

@darshankawar
Thank you for quick reply. I noticed that it manages to decode the top-level Timestamp but fails to decode timestamp in the nested objects. So my class definition above is probably invalid, here is a more accurate one:

{
    invoice: {
        createdTimestamp: Timestamp(....),
    },
    createdTimestamp: Timestamp(....),
}
class Session {
    Invoice invoice;
    Timestamp createdTimestamp; //this is fine

    static Session of(Map<String, dynamic> map) {
        return Session(
            Invoice.of(map["invoice"])
        );
    }
}

class Invoice {
    Timestamp createdTimestamp; //this breaks

    static Invoiceof(Map<String, dynamic> map) {
        return Session(map["createdTimestamp"]);
    }
}

I'll try to create a runnable code sample 👋

@google-oss-bot google-oss-bot added Needs Attention This issue needs maintainer attention. and removed blocked: customer-response Waiting for customer response, e.g. more information was requested. labels Feb 1, 2024
@yarmel
Copy link

yarmel commented Feb 1, 2024

Have the same error in my function

static DateTime? toDate(dynamic date) {
    if (date == null) {
      return null;
    } else if (date is DateTime) {
      return date;
    } else if (date is String) {
      return DateTime.parse(date);
    } else if (date is int) {
      final int length = date.toString().length;

      return (length == 13)
          ? DateTime.fromMillisecondsSinceEpoch(date)
          : DateTime.fromMicrosecondsSinceEpoch(date);
    } else {
    // HERE when try to convert Timestamp to DateTime
    // Expected a value of type 'DateTime?', but got one of type 'LegacyJavaScriptObject'
      return date.toDate();
    }

@AVancans
Copy link
Author

AVancans commented Feb 2, 2024

@darshankawar

Here is the code that can reproduce the error:

Object:

{
    nested: {
        timestamp: ....
    },
    timestamp: ...
}

Code:

  DocumentSnapshot doc = await FirebaseFirestore.instance.doc("_public/sample").get();
  Timestamp topLevel = doc.get("timestamp");
  print(topLevel);
  Timestamp nested = doc.get("nested.timestamp");
  print(nested);

Log:

Top level: Timestamp(seconds=1706803200, nanoseconds=45000000)
Error: Expected a value of type 'Timestamp', but got one of type 'LegacyJavaScriptObject'
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 294:3       throw_
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 127:3       castError
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 818:12  cast
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/classes.dart 652:14     as_C
packages/bemo_pos/main.dart 33:17                                                 tryTimestamp
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 45:50                <fn>
dart-sdk/lib/async/zone.dart 1661:54                                              runUnary
dart-sdk/lib/async/future_impl.dart 162:18                                        handleValue
dart-sdk/lib/async/future_impl.dart 846:44                                        handleValueCallback
dart-sdk/lib/async/future_impl.dart 875:13                                        _propagateToListeners
dart-sdk/lib/async/future_impl.dart 647:5                                         [_completeWithValue]
dart-sdk/lib/async/future_impl.dart 721:7                                         callback
dart-sdk/lib/async/schedule_microtask.dart 40:11                                  _microtaskLoop
dart-sdk/lib/async/schedule_microtask.dart 49:5                                   _startMicrotaskLoop
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 181:7                <fn>

@FedeRotoli
Copy link

I encountered the same error working with timestamp.

@darshankawar darshankawar added resolution: fixed A fix has been merged or is pending merge from a PR. and removed Needs Attention This issue needs maintainer attention. triage Issue is currently being triaged. labels Feb 2, 2024
@yarmel
Copy link

yarmel commented Feb 2, 2024

Just updated all dependencies to the latest versions, but the problem with nested Map with DateTime is still present. Returns an error: "Expected a value of type 'DateTime?', but got one of type 'LegacyJavaScriptObject'. "

The problem is marked as resolved.
How to fix it?

  firebase_core: ^2.25.3
  firebase_auth: ^4.17.3
  firebase_storage: ^11.6.4
  firebase_remote_config: ^4.3.12
  firebase_messaging: ^14.7.14
  firebase_crashlytics: ^3.4.13
  firebase_analytics: ^10.8.4
  cloud_functions: ^4.6.4
  cloud_firestore: ^4.15.3

@AVancans
Copy link
Author

AVancans commented Feb 2, 2024

@yarmel

It's working for me now.

  firebase_core: ^2.25.3
  cloud_firestore: ^4.15.3
  firebase_auth: ^4.17.0

Make sure you run pub get and restart any running debug services

@yarmel
Copy link

yarmel commented Feb 2, 2024

@yarmel

It's working for me now.

  firebase_core: ^2.25.3
  cloud_firestore: ^4.15.3
  firebase_auth: ^4.17.0

Make sure you run pub get and restart any running debug services

Unfortunately, not working

  firebase_core: ^2.25.3
  firebase_auth: ^4.17.3
  firebase_storage: ^11.6.4
  firebase_remote_config: ^4.3.12
  firebase_messaging: ^14.7.14
  firebase_crashlytics: ^3.4.13
  firebase_analytics: ^10.8.4
  cloud_functions: ^4.6.4
  cloud_firestore: ^4.15.3

Steps:

  1. Flutter Clean
  2. Flutter Pub Get
  3. Flutter Pub Upgrade
  4. Build web

Result:

Expected a value of type 'DateTime?', but got one of type 'LegacyJavaScriptObject'

Occurs when trying to convert to DateTime

  static DateTime? objToDate(dynamic date) {
 return date.toDate();
 }

Map structure in the firestore:
Screenshot 2024-02-02 at 17 10 16

@firebase firebase locked and limited conversation to collaborators Mar 4, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
resolution: fixed A fix has been merged or is pending merge from a PR. type: bug Something isn't working
Projects
None yet
6 participants