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] Uncaught TypeError: Cannot read properties of undefined (reading 'apply') #9106

Closed
ViniciusSossela opened this issue Jul 15, 2022 · 33 comments · Fixed by #9246
Assignees
Labels
platform: web Issues / PRs which are specifically for web. plugin: cloud_firestore type: bug Something isn't working type: crash A compile error or crash

Comments

@ViniciusSossela
Copy link

Bug report

Some firestore method/execution on web are trowing the following error:

Uncaught TypeError: Cannot read properties of undefined (reading 'apply')
    at Object.bX (main.dart.js:9188:23)
    at aB6.a2m (main.dart.js:83433:16)
    at cyX.$1 (main.dart.js:83482:23)
    at Object.cyU (main.dart.js:15840:18)
    at cyW.$2 (main.dart.js:95452:21)
    at k9.O (main.dart.js:68411:18)
    at Object.bU (main.dart.js:54616:24)
    at Object.cyU (main.dart.js:15838:3)
    at Object.rx (main.dart.js:12531:10)
    at bSW.eP (main.dart.js:83588:6)

This only happens after deploying on web, in debug everything is fine;

Steps to reproduce

 firebase_core: ^1.18.0
  cloud_firestore: ^3.1.18

Additional context


Already have notice that this error happens when using firestore batch.
Example:

final _fireBatch = _firestore.batch();
final _ref = _firestore
    .collection('xxxx')
    .doc(xx)
    .collection(xxx);

for (var centerCost in itens) {
  _fireBatch.set(_ref.doc(), entity.toJson());
}

await _fireBatch.commit();

Flutter doctor

Run flutter doctor and paste the output below:

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel unknown, 3.0.5, on macOS 12.1 21C52 darwin-x64, locale en-BR)
[✓] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1)
[✓] Xcode - develop for iOS and macOS (Xcode 13.3)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2021.1)
[✓] VS Code (version 1.69.1)
[✓] Connected device (2 available)
[✓] HTTP Host Availability

• No issues found!

@ViniciusSossela ViniciusSossela added Needs Attention This issue needs maintainer attention. type: bug Something isn't working labels Jul 15, 2022
@darshankawar darshankawar added the triage Issue is currently being triaged. label Jul 15, 2022
@darshankawar
Copy link

Thanks for the report @ViniciusSossela
Can you upgrade your firebase plugins to latest and try again ?

cloud_firestore: ^3.3.0
firebase_core: ^1.19.2
https://pub.dev/packages/cloud_firestore_web/install

Also, can you provide index.html along with a minimal code sample that triggers this error ?

@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 Jul 15, 2022
@ViniciusSossela
Copy link
Author

Hi @darshankawar thanks to reply.

Just looked at my pub.lock:

 cloud_firestore_web:
    dependency: transitive
    description:
      name: cloud_firestore_web
      url: "https://pub.dartlang.org"
    source: hosted
    version: "2.7.0"

....
firebase_core:
    dependency: "direct main"
    description:
      name: firebase_core
      url: "https://pub.dartlang.org"
    source: hosted
    version: "1.19.2"

So basically it was using the last firebase core version, but I also made test with the latest firebase versions and the same happens.

Here are the index.html

<!DOCTYPE html>
<html>
<head>
  <!--
    If you are serving your web app in a path other than the root, change the
    href value below to reflect the base path you are serving from.

    The path provided below has to start and end with a slash "/" in order for
    it to work correctly.

    For more details:
    * https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base

    This is a placeholder for base href that will be replaced by the value of
    the `--base-href` argument provided to `flutter build`.
  -->
  <base href="$FLUTTER_BASE_HREF">

  <meta charset="UTF-8">
  <meta content="IE=Edge" http-equiv="X-UA-Compatible">
  <meta name="description" content="XXXXXX">

  <!-- iOS meta tags & icons -->
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="apple-mobile-web-app-title" content="XXXXXX ">
  <link rel="apple-touch-icon" href="icons/Icon-192.png">

  <!-- Favicon -->
  <link rel="icon" type="image/png" href="favicon.png"/>

  <title>XXXXXX</title>
  <link rel="manifest" href="manifest.json">

  <script src="https://maps.googleapis.com/maps/api/js?key=XXXXXXXXXXXX"></script>
  
  <script>
    // The value below is injected by flutter build, do not touch.
    var serviceWorkerVersion = null;
  </script>
  <!-- This script adds the flutter initialization JS code -->
  <script src="flutter.js" defer></script>
</head>
<body>
  <script>
    window.addEventListener('load', function(ev) {
      // Download main.dart.js
      _flutter.loader.loadEntrypoint({
        serviceWorker: {
          serviceWorkerVersion: serviceWorkerVersion,
        }
      }).then(function(engineInitializer) {
        return engineInitializer.initializeEngine();
      }).then(function(appRunner) {
        return appRunner.runApp();
      });
    });
  </script>
</body>
</html>

and here the main.dart firebase initializer:

  await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);

@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 Jul 15, 2022
@darshankawar
Copy link

Can you try adding below imports in index file and see if they help ?

<script src="https://www.gstatic.com/firebasejs/9.9.0/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/9.9.0/firebase-firestore.js"></script>

@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 Jul 15, 2022
@ViniciusSossela
Copy link
Author

ViniciusSossela commented Jul 15, 2022

@darshankawar Just added as you suggested, but the same happens:

 <script src="https://maps.googleapis.com/maps/api/js?key=XXXXXXX"></script>
  <script src="https://www.gstatic.com/firebasejs/9.9.0/firebase-app.js"></script>
  <script src="https://www.gstatic.com/firebasejs/9.9.0/firebase-firestore.js"></script>
  
  <script>
    // The value below is injected by flutter build, do not touch.
    var serviceWorkerVersion = null;
  </script>
  <!-- This script adds the flutter initialization JS code -->
  <script src="flutter.js" defer></script>
</head>
<body>
  <script>
    window.addEventListener('load', function(ev) {
      // Download main.dart.js
      _flutter.loader.loadEntrypoint({
        serviceWorker: {
          serviceWorkerVersion: serviceWorkerVersion,
        }
      }).then(function(engineInitializer) {
        return engineInitializer.initializeEngine();
      }).then(function(appRunner) {
        return appRunner.runApp();
      });
    });
  </script>
</body>

The errors that happens with the scripts on index.html

Uncaught SyntaxError: Unexpected token 'export' (at VM104 firebase-app.js:2350:1)

VM105 firebase-firestore.js:1 Uncaught SyntaxError: Cannot use import statement outside a module (at VM105 firebase-firestore.js:1:1)

main.dart.js:4951 Uncaught TypeError: Cannot read properties of undefined (reading 'apply')
    at Object.bX (main.dart.js:9188:23)
    at aB6.a2m (main.dart.js:83433:16)
    at cyX.$1 (main.dart.js:83482:23)
    at Object.cyU (main.dart.js:15840:18)
    at cyW.$2 (main.dart.js:95452:21)
    at k9.O (main.dart.js:68411:18)
    at Object.bU (main.dart.js:54616:24)
    at Object.cyU (main.dart.js:15838:3)
    at Object.rx (main.dart.js:12531:10)
    at bSW.eP (main.dart.js:83588:6)

@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 Jul 15, 2022
@ViniciusSossela
Copy link
Author

It seems a problem with the firebase_core_web 1.7.0

Solved the issue by downgrading to the following package version:

firebase_core: 1.13.1
firebase_core_web: 1.6.1

@darshankawar
Copy link

Thanks for the update. Using below dependency plugin and trying to run plugin's example on web, I am getting another error which is pointing to irebase_core_web 1.7.0:

  firebase_core: ^1.19.2
  cloud_firestore: ^3.3.0

Error: Assertion failed: file:///Users/dhs/Documents/Fluttersdk/flutter/.pub-cache/hosted/pub.dartlang.org/firebase_core_web-1.7.0/lib/src/firebase_core_web.dart:207:11

Labeling it based on the report.

@binlebin
Copy link

I have the same error: working fine in debug mode but error in deployment.

@Lyokone Lyokone self-assigned this Jul 22, 2022
@Lyokone
Copy link
Contributor

Lyokone commented Jul 22, 2022

I just tried batch on debug and release mode and works fine for both.
Do you have the error with batch too @binlebin ?

@binlebin
Copy link

@Lyokone I did get the bug on a simple get query in deployment only. Sample code below:

Query query = postsRef.orderBy('created_time', descending: true);
if (pageKey != null) {
query =
query.startAfterDocument(_postToSnapshotMap[pageKey.uniqueCode]!);
}

query = query.where('followers', arrayContains: _user!.uid);
query = query.limit(_pageSize);

if (!isAdmin) {
query = query.where('blocked', isEqualTo: false);
}

print('Constructed query $query');

QuerySnapshot querySnapshot = await query.get();

print('Query completed');

In debug mode, everything works fine. In deployment, the last print statement was never reached. It seems query.get() is where the bug is.

I needed to downgrade all firestore packages:

pub outdated output:

Package Name Current Upgradable Resolvable Latest

direct dependencies:
cloud_firestore *3.2.1 3.4.0 3.4.0 3.4.0
cloud_functions *3.3.1 3.3.3 3.3.3 3.3.3
firebase_analytics *9.1.12 9.2.1 9.2.1 9.2.1
firebase_app_check *0.0.6+16 0.0.6+18 0.0.6+18 0.0.6+18
firebase_auth *3.4.1 3.5.0 3.5.0 3.5.0
firebase_core *1.19.1 1.20.0 1.20.0 1.20.0
firebase_messaging *11.4.4 *11.4.4 12.0.1 12.0.1
firebase_storage *10.3.1 10.3.3 10.3.3 10.3.3

flutter doctor -v

[✓] Flutter (Channel stable, 3.0.5, on macOS 12.4 21F79 darwin-arm, locale en-US)
• Flutter version 3.0.5 at /Users/anh/Dev/flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision f1875d570e (9 days ago), 2022-07-13 11:24:16 -0700
• Engine revision e85ea0e79c
• Dart version 2.17.6
• DevTools version 2.12.2

[✓] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1)
• Android SDK at /Users/anh/Library/Android/sdk
• Platform android-32, build-tools 32.1.0-rc1
• Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
• All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 13.4.1)
• Xcode at /Applications/Xcode.app/Contents/Developer
• CocoaPods version 1.11.3

[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2021.2)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)

[✓] VS Code (version 1.69.1)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension can be installed from:
🔨 https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter

[✓] Connected device (3 available)
• Anh’s iPhone X (mobile) • 1db0cd86988ec78b646876c35013e9e345a5dcd8 • ios • iOS 15.5 19F77
• macOS (desktop) • macos • darwin-arm64 • macOS 12.4 21F79 darwin-arm
• Chrome (web) • chrome • web-javascript • Google Chrome 103.0.5060.134

[✓] HTTP Host Availability
• All required HTTP hosts are available

@m-j-g
Copy link

m-j-g commented Jul 25, 2022

I'm in dependency hell trying to get around this issue.

If I try to downgrade firebase_auth to 3.4.1 I get:

../flutter/.pub-cache/hosted/pub.dartlang.org/firebase_auth-3.4.1/lib/src/recaptcha_verifier.dart:57:27: Error: Required named parameter 'auth' must be provided.
_factory.delegateFor(
^

If I upgrade firebase_auth to 3.4.2 or higher than it requires firebase_core_web: 1.7 . If I upgrade firebase_core_web 1.7 then I inherit the issues listed here. What is the fix?

@m-j-g
Copy link

m-j-g commented Jul 25, 2022

I fixed one 3.4.1 auth issue with #9216, but now I just have more issues:

Target dart2js failed: Exception: Warning: The 'dart2js' entrypoint script is deprecated, please use 'dart compile js' instead.
../flutter/.pub-cache/hosted/pub.dartlang.org/cloud_firestore_web-2.6.19/lib/cloud_firestore_web.dart:101:14:
Error: The method 'FirebaseFirestoreWeb.runTransaction' has fewer named arguments than those of overridden method 'FirebaseFirestorePlatform.runTransaction'.
Future<T?> runTransaction(TransactionHandler transactionHandler,
^

This just seems like a bottomless pit of issues trying to downgrade various things. I really can't figure out the right combinations. The one listed above causes errors.

I'm in a bind because we just released our new mobile version last night, and I had to put the web update or else the two wouldn't play well together. However, as mentioned here, I have functionality that works in debug, but breaks on release build, and so I have full features that just don't load. This issue has been open for 11 days which leads me to believe that this isn't urgent. I just am at a loss here, I have been working on this all day and only running into more and more issues as I try to fix this original issue. This is supposed to be the stable branch!

@Lyokone
Copy link
Contributor

Lyokone commented Jul 25, 2022

You might want to run flutter clean on your project. As you can see on pub.dev, the latest version of cloud_firestore_web is 2.8.1 : https://pub.dev/packages/cloud_firestore_web

@m-j-g
Copy link

m-j-g commented Jul 25, 2022

You might want to run flutter clean on your project. As you can see on pub.dev, the latest version of cloud_firestore_web is 2.8.1 : https://pub.dev/packages/cloud_firestore_web

Yeah, I'm on my 100th flutter clean today. And yes, I wish the latest versions didn't have these issues so that we could use them.

@Lyokone
Copy link
Contributor

Lyokone commented Jul 25, 2022

I've tried all the combinations that you provided @binlebin and they all work in debug and release on the Example app of FlutterFire.
Can anyone provide a full repro example so we can fix this quickly?

@ViniciusSossela
Copy link
Author

@Lyokone

Which firebase_core_web version are you using?

@m-j-g
Copy link

m-j-g commented Jul 25, 2022

For what it's worth. I get this error when I use the Query option "startAfterDocument()" in my query. Like everyone else says, this works find in debug builds, but gives the mentioned error message in release builds.

@ViniciusSossela
Copy link
Author

@m-j-g did you tried firebase_core_web: 1.6.1 ?

@binlebin
Copy link

binlebin commented Jul 25, 2022

I also notice the error might be because of "startAfterDocument()" as my first page loads fine.

@m-j-g You might want to reuse old pubspec.lock of a version that was working fine.

@m-j-g
Copy link

m-j-g commented Jul 25, 2022

@m-j-g did you tried firebase_core_web: 1.6.1 ?

I've been trying so hard to use it. haha. But it requires me to downgrade the auth library, and then it starts a bunch of other dependency / outdated issues.

I also notice the error might be because of "startAfterDocument()" as my first page loads fine.

@m-j-g You might want to reuse old pubspec.lock of a version that was working fine.

That's a good point. I have the same experience, initial list data loads fine, but when I query for more using startAfterDocument() the error happens. (only on web release, once again works fine on web debug and on iOS/android)

@ViniciusSossela
Copy link
Author

@m-j-g Yeah, I got the same. This bug got me crazy man....

Did you also tried the new version firebase_core_web: 1.7.1

@m-j-g
Copy link

m-j-g commented Jul 25, 2022

@m-j-g Yeah, I got the same. This bug got me crazy man....

Did you also tried the new version firebase_core_web: 1.7.1

Yeah, still has the issue.

@ViniciusSossela
Copy link
Author

@m-j-g try all these firebase dependencies..

  firebase_core: ^1.19.1
  firebase_core_web: 1.6.6
  cloud_firestore: ^3.2.1
  cloud_firestore_platform_interface: 5.5.10
  firebase_auth: ^3.4.1
  firebase_auth_platform_interface: 6.3.1
  firebase_storage: ^10.3.1
  firebase_analytics: ^9.1.12
  firebase_analytics_platform_interface: 3.1.10
  firebase_crashlytics: ^2.8.2
  firebase_messaging: ^11.4.4
  firebase_remote_config: ^0.10.0+4
  firebase_in_app_messaging: ^0.5.0+8

To me it worked.

@m-j-g
Copy link

m-j-g commented Jul 25, 2022

@m-j-g try all these firebase dependencies..

  firebase_core: ^1.19.1
  firebase_core_web: 1.6.6
  cloud_firestore: ^3.2.1
  cloud_firestore_platform_interface: 5.5.10
  firebase_auth: ^3.4.1
  firebase_auth_platform_interface: 6.3.1
  firebase_storage: ^10.3.1
  firebase_analytics: ^9.1.12
  firebase_analytics_platform_interface: 3.1.10
  firebase_crashlytics: ^2.8.2
  firebase_messaging: ^11.4.4
  firebase_remote_config: ^0.10.0+4
  firebase_in_app_messaging: ^0.5.0+8

To me it worked.

It works! Thanks man, I really appreciate that.

@Lyokone
Copy link
Contributor

Lyokone commented Jul 26, 2022

Hey, I can finally reproduce the error, thanks @binlebin, I'll check what can cause the compilation in JS not to work properly

@Lyokone
Copy link
Contributor

Lyokone commented Jul 27, 2022

We have raised the issue internally with Google to see what happens with dart2js. We'll keep you posted here.
Even if it's not ideal, you can always flutter build --debug in the time being if you need some functionalities from the latest version.

@Lyokone
Copy link
Contributor

Lyokone commented Jul 28, 2022

It was fixed with 3.6.1

@LAPPIT
Copy link

LAPPIT commented Jul 28, 2022

Tested the new version and it worked fine :) Thanks!

@bottle-rocket-11
Copy link

3.6.1 fixed the auth persistence bug. I am still experiencing the Uncaught TypeError: Cannot read properties of undefined (reading 'apply') in production. Works fine in debug

@shadowboxingskills
Copy link

+1. production build still breaks due to this undefined (reading 'apply') bug

@ViniciusSossela
Copy link
Author

ViniciusSossela commented Jul 29, 2022

@hugopretorius914 @shadowboxingskills have you tried with these versions ?

cloud_firestore: ^3.4.2
cloud_firestore_web: ^2.8.2

The fix seems to be in the cloud_firestore
https://github.com/firebase/flutterfire/pull/9246/files


@Lyokone Have this PR also fixed the problem with batch transactions ?

final _fireBatch = _firestore.batch();
final _ref = _firestore
    .collection('xxxx')
    .doc(xx)
    .collection(xxx);

for (var item in itens) {
  _fireBatch.set(_ref.doc(), entity.toJson());
}

await _fireBatch.commit();

@shadowboxingskills
Copy link

@hugopretorius914 @shadowboxingskills have you tried with these versions ?

cloud_firestore: ^3.4.2
cloud_firestore_web: ^2.8.2

Yes, upgraded to these latest versions above but crash still occurs in production (not in debug)

@bottle-rocket-11
Copy link

@hugopretorius914 @shadowboxingskills have you tried with these versions ?

cloud_firestore: ^3.4.2
cloud_firestore_web: ^2.8.2

Yes, I get the same as @shadowboxingskills the crash still happens in production with all those version but works fine in debug

@shadowboxingskills
Copy link

It has been fixed in the latest release earlier today.
Thank you

@firebase firebase locked and limited conversation to collaborators Aug 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
platform: web Issues / PRs which are specifically for web. plugin: cloud_firestore type: bug Something isn't working type: crash A compile error or crash
Projects
None yet
9 participants