Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
210 views
in Technique[技术] by (71.8m points)

node.js - Firebase Functions onCall not working in Dart with firebase_functions_interop

Update: I was able to find the source of the problem - it appears to be related to a circular reference in lodash which happens when serialising the response. Apparently I need to return a specific kind of promise, which I could not figure out how to do yet.

End of Update.

I am trying to get the official cloud function example from pub to work with dart using the packagefirebase_functions_interop which converts dart code to a deployable node based cloud function. I followed this tutorial to setup the build runner etc (and their example is working for me).

Now on to the callable functions which are what this question is all about.

This is a working example using JavaScript:

// JavaScript
const functions = require('firebase-functions');

exports.listFruit = functions.https.onCall(() => {
    return [
        "Apple",
        "Banana",
        "Cherry",
        "Date",
        "Fig",
        "Grapes"
    ];
});

This is what I came up with using dart:

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

void main() {
  functions['listFruit'] = functions.https.onCall((data, context) => listFruit);
}
FutureOr<List<String>> listFruit(dynamic data, CallableContext context) {
  return ['Apple', 'Banana', 'Cherry', 'Date', 'Fig', 'Grapes'];
}

But upon execution I get this console output and a 500 error:

i  functions: Beginning execution of "listFruit"
>  Response cannot be encoded. Closure 'minified:ns' dh {
>    '$initialize': [Function: dh],
>    constructor: [Function: static_tear_off],
>    '$static_name': 'ns',
>    '$S': [Function (anonymous)],
>    '$2': [Function: ns] {
>      '$callName': '$2',
>      '$R': 2,
>      '$D': null,
>      '$stubName': 'ns',
>      '$tearOff': [Function (anonymous)]
>    },
>    '$C': [Function: ns] {
>      '$callName': '$2',
>      '$R': 2,
>      '$D': null,
>      '$stubName': 'ns',
>      '$tearOff': [Function (anonymous)]
>    },
>    '$R': 2,
>    '$D': null
>  }
>  {"severity":"ERROR","message":"Unhandled error Error: Invalid response, check logs for details
    at Object.d (/test_dart_functions/functions/build/index.dart.js:183:3)
    at h3.$2 (/test_dart_functions/functions/build/index.dart.js:4066:9)
    at Object.m4 (/test_dart_functions/functions/build/index.dart.js:138:112)
    at mE (/test_dart_functions/functions/build/index.dart.js:1872:10)
    at /test_dart_functions/functions/build/index.dart.js:1866:42
    at newHandler (/usr/local/Cellar/firebase-cli/9.1.0/libexec/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:286:16)
    at func (/test_dart_functions/functions/node_modules/firebase-functions/lib/providers/https.js:273:32)
    at /test_dart_functions/functions/node_modules/firebase-functions/lib/providers/https.js:293:44
    at cors (/test_dart_functions/functions/node_modules/cors/lib/index.js:188:7)
    at /test_dart_functions/functions/node_modules/cors/lib/index.js:224:17 {
  dartException: HttpsError: Invalid response, check logs for details
      at Object.n9 (/test_dart_functions/functions/build/index.dart.js:1825:15)
      at bD.aO (/test_dart_functions/functions/build/index.dart.js:4023:24)
      at h3.$2 (/test_dart_functions/functions/build/index.dart.js:4066:13)
      at Object.m4 (/test_dart_functions/functions/build/index.dart.js:138:112)
      at mE (/test_dart_functions/functions/build/index.dart.js:1872:10)
      at /test_dart_functions/functions/build/index.dart.js:1866:42
      at newHandler (/usr/local/Cellar/firebase-cli/9.1.0/libexec/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:286:16)
      at func (/test_dart_functions/functions/node_modules/firebase-functions/lib/providers/https.js:273:32)
      at /test_dart_functions/functions/node_modules/firebase-functions/lib/providers/https.js:293:44
      at cors (/test_dart_functions/functions/node_modules/cors/lib/index.js:188:7) {
    code: 'internal',
    details: "Closure 'minified:ns'",
    httpErrorCode: { canonicalName: 'INTERNAL', status: 500 }
  }
}"}
i  functions: Finished "listFruit" in ~1s

I'm really looking forward to writing all my cloud functions in dart - except for onCall the others seem to be working OK.

Any help would be greatly appreciated!

question from:https://stackoverflow.com/questions/65890791/firebase-functions-oncall-not-working-in-dart-with-firebase-functions-interop

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...