pulyaevskiy / firebase-functions-interop

Firebase Functions Interop Library for Dart.
BSD 3-Clause "New" or "Revised" License
191 stars 52 forks source link

document.onUpdate TypeError: Instance of 'UnknownJavaScriptObject': type 'UnknownJavaScriptObject' is not a subtype of type 'Change0<DocumentSnapshot0>' #25

Closed zirinisp closed 5 years ago

zirinisp commented 6 years ago

Running the following code when on document update generates an error

import 'dart:async';
import 'package:firebase_functions_interop/firebase_functions_interop.dart';

void main() {
  functions['updateDocument'] = FirebaseFunctions.firestore.document('{collection}/{document}').onUpdate(onUpdate);
}

FutureOr<void> onUpdate(Change<DocumentSnapshot> change, EventContext context) {
  DocumentSnapshot snapshot = change.after;
  DocumentData updatedDocument = snapshot.data;
  DocumentData previousDocument = change.before.data;
  String updatedAtKey = "updatedAt";

  print('Got Snapshots');

  // If 'updatedAt' is different, that means we have already updated it
  DateTime updatedDocumentTimestamp = updatedDocument.getDateTime(updatedAtKey);
  DateTime previousDocumentTimestamp = previousDocument.getDateTime(updatedAtKey);

  print('Got Timestamps');

  if ((updatedDocumentTimestamp != null) && (previousDocumentTimestamp != null)) {
    if (updatedDocumentTimestamp != previousDocumentTimestamp) {
      print('Timestamps Different Returning');
      return null;
    }
  }

  UpdateData newData = new UpdateData();
  DateTime now = new DateTime.now();
  newData.setDateTime(updatedAtKey, now);

  print('Set update key');

  return snapshot.reference.updateData(newData);
}

I get the following error.

TypeError: Instance of 'UnknownJavaScriptObject': type 'UnknownJavaScriptObject' is not a subtype of type 'Change0<DocumentSnapshot0>'
    at wrapException (/user_code/build/node/index.dart.js:2674:17)
    at assertSubtype (/user_code/build/node/index.dart.js:3428:15)
    at DocumentBuilder_onUpdate_wrapper.dart.DocumentBuilder_onUpdate_wrapper.call$2 (/user_code/build/node/index.dart.js:8971:50)
    at Primitives_applyFunctionWithPositionalArguments (/user_code/build/node/index.dart.js:2622:28)
    at dart._callDartFunctionFast (/user_code/build/node/index.dart.js:8359:14)
    at /user_code/build/node/index.dart.js:8348:18
    at Object.<anonymous> (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:112:27)
    at next (native)
    at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71
    at __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12)

Could be related to https://github.com/pulyaevskiy/firebase-functions-interop/issues/23

My versions are:

dart --version
Dart VM version: 2.0.0-dev.59.0 (Tue May 29 22:11:36 2018 +0200) on "macos_x64"
node --version
v9.5.0
firebase --version
3.18.5

And my pubspec.yaml is:

name: surf_spotter_functions
description: My project functions
version: 0.0.1

environment:
  sdk: '>=2.0.0-dev <2.0.0'

dependencies:
  # Firebase Functions bindings
  firebase_functions_interop: ^1.0.0-dev

dev_dependencies:
  # Needed to compile Dart to valid Node.js module.
  build_runner: ^0.7.9
  build_node_compilers: ^0.1.0
zirinisp commented 6 years ago

I am getting the same error using onWrite instead of onUpdate

pulyaevskiy commented 6 years ago

Thanks for the report!

Looks like dart2js started doing more type checks since dev.52. I suspect you have checked flag in your build.yaml? E.g.:

    builders:
      build_node_compilers|entrypoint:
        generate_for:
        - node/**
        options:
          compiler: dart2js
          # List any dart2js specific args here, or omit it.
          dart2js_args:
          - --checked # <-- This

If you remove dart2js_args block (last two lines) the error should go away. This would be a bit of a workaround (though in production you'd want to disable checked mode anyway).

zirinisp commented 6 years ago

Thank you for the reply. I changed my build.yaml file to the following:

targets:
  $default:
    sources:
      - "node/**"
      - "lib/**"
    builders:
      build_node_compilers|entrypoint:
        generate_for:
        - node/**
        options:
          compiler: dart2js
          # List any dart2js specific args here, or omit it.
          #dart2js_args:
          #- --checked

I also tried the following (just in case it makes a difference:

targets:
  $default:
    sources:
      - "node/**"
      - "lib/**"
    builders:
      build_node_compilers|entrypoint:
        generate_for:
        - node/**
        options:
          compiler: dart2js
          # List any dart2js specific args here, or omit it.
          dart2js_args:
          #- --checked

Unfortunately, I am getting the same error.

Bear in mind that I do not get an error for onCreate, which uses DocumentSnapshot snapshot, instead of Change<DocumentSnapshot> change.

pulyaevskiy commented 6 years ago

Ok, thanks! I’ll do some more investigation today.

pulyaevskiy commented 6 years ago

I just added a test case for Firestore onWrite trigger and I must say that disabling --checked mode in dart2js fixes the issue for me. Enabling it back gives the same stack trace as you provided.

Not sure why you still getting the error. Could you double check and redeploy with no dart2js_args section in build.yaml? Sorry for the trouble.

pulyaevskiy commented 6 years ago

Also, I just submitted https://github.com/dart-lang/sdk/issues/33313 hoping to get some help from the Dart team.

alextekartik commented 6 years ago

Le ven. 1 juin 2018 à 20:32, Anatoly Pulyaevskiy notifications@github.com a écrit :

Also, I just submitted dart-lang/sdk#33313 https://github.com/dart-lang/sdk/issues/33313 hoping to get some help from the Dart team.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/pulyaevskiy/firebase-functions-interop/issues/25#issuecomment-393971182, or mute the thread https://github.com/notifications/unsubscribe-auth/ABpC1kusHBv4CS1He-GAQFqs5DaPKKWkks5t4Yi-gaJpZM4UUzZX .

pulyaevskiy commented 6 years ago

We got some help from the team. Looks like --checked flag might go away with Dart 2. Given that I switched to --preview-dart-2 flag as was suggested in the SDK issue, and it worked. I did find a different strong mode issue, which was easy to fix, will publish an update later today.

@zirinisp let me know if redeploying without --checked flag still doesn't work for you.

Update: 1.0.0-dev.6.0 is published, which includes strong mode fixes. Building with --preview-dart-2 should work now.

zirinisp commented 6 years ago

I updated dart to 2.0.0-dev.60.0, commented out --checked flag, run pub get, run pub update, run pub run build_runner build and then uploaded to firestore firebase deploy --only functions.

So I am now using your latest version 1.0.0-dev6.0.

I am getting the following for deploy so everything was uploaded:

i  deploying functions
i  functions: ensuring necessary APIs are enabled...
✔  functions: all necessary APIs are enabled
i  functions: preparing functions directory for uploading...
i  functions: packaged functions (38.98 MB) for uploading
✔  functions: functions folder uploaded successfully
i  functions: updating function createDocument...
i  functions: updating function updateDocument...
✔  functions[createDocument]: Successful update operation. 
✔  functions[updateDocument]: Successful update operation. 

✔  Deploy complete!

I go to my firestore database and update something. That triggers the function and I get the error.

So removing the --checked flag did not help

Using --preview-dart-2

I could not find a way to use it. Please help me with the syntax. pub run build_runner build --preview-dart-2 did not work.

I read somewhere that this option is not enabled by default.

Updating build_runner and build_node_compilers

I then went on my pubspec and updated it to:

dev_dependencies:
  # Needed to compile Dart to valid Node.js module.
  build_runner: ^0.8.9. # was 0.7.9
  build_node_compilers: ^0.1.3 # was 0.1.0

I am still getting the same error. So it is not related to the latest dependencies.

pulyaevskiy commented 6 years ago

I could not find a way to use it.

Sorry I wasn't clear. You just need to replace --checked with --preview-dart-2 in build.yaml:

targets:
  $default:
    sources:
      - "node/**"
      - "lib/**"
    builders:
      build_node_compilers|entrypoint:
        generate_for:
        - node/**
        options:
          compiler: dart2js
          dart2js_args:
          - --preview-dart-2

I'm going to guess now but it seems like --checked is still getting pickup up from somewhere.

  1. Can you share folder structure of your project?
  2. Are there any other build.yaml files around?
  3. Which command you use to build your project?

Hope we can get it solved soon!

zirinisp commented 6 years ago

Hello and thank you for your help. We should find a way to fix it at the end.

  1. So the structure of my project is the following:
screen shot 2018-06-13 at 09 53 47
  1. There is only one and it is the same as the one you send me on your last message.

  2. I am running the following commands:

To build run:

pub run build_runner build

To deploy to firebase:

firebase deploy --only functions

I have it on a private repo. If it helps, I could also share it with you.

zirinisp commented 6 years ago

@pulyaevskiy any news on the issue?

pulyaevskiy commented 6 years ago

Sorry for delay here. I tried copy-pasting your function and running on test account and it seems to be working fine with no errors.

So, I'm not able to reproduce the error at this point. I'm almost out of option at this point. One last thing I can ask if you can share output of pub run build_runner build command?

pulyaevskiy commented 5 years ago

@zirinisp I'm closing this issue for now but feel free to leave a comment or reopen if it's still unresolved.