Ошибка: невозможно обработать вызов функции в Cloud Functions Firestore - PullRequest
0 голосов
/ 06 мая 2020

Я хочу получить документ в форме текущего uid пользователя, переданного на стороне клиента. Я пробовал использовать functions.onRequest и успешно получил результат, но у меня возникла ошибка при использовании метода onCall.

Cloud Function

exports.getUserData = functions.https.onCall((data) => {
    const test = data.text;
    db.collection('users').doc(test).get().then((value)=>{
        return value.data();  
    }).catch((e)=>{
        return ("Error in CFunction"+e); 
    })

});

Client-side Call

Future _callable()async{
    FirebaseUser currentUser = await _auth.currentUser();
    final uid = currentUser.uid;
    final HttpsCallable callable =
        CloudFunctions.instance.getHttpsCallable(functionName: 'getUserData');
        var data = await callable.call(uid);
        print(data.data);
  }

Я получаю ошибку


E/MethodChannel#cloud_functions(12458): Failed to handle method call
E/MethodChannel#cloud_functions(12458): java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Map
E/MethodChannel#cloud_functions(12458):         at io.flutter.plugins.firebase.cloudfunctions.CloudFunctionsPlugin.onMethodCall(CloudFunctionsPlugin.java:37)
E/MethodChannel#cloud_functions(12458):         at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:226)
E/MethodChannel#cloud_functions(12458):         at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:85)
E/MethodChannel#cloud_functions(12458):         at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:631)
E/MethodChannel#cloud_functions(12458):         at android.os.MessageQueue.nativePollOnce(Native Method)
E/MethodChannel#cloud_functions(12458):         at android.os.MessageQueue.next(MessageQueue.java:334)
E/MethodChannel#cloud_functions(12458):         at android.os.Looper.loop(Looper.java:174)
E/MethodChannel#cloud_functions(12458):         at android.app.ActivityThread.main(ActivityThread.java:6939)
E/MethodChannel#cloud_functions(12458):         at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#cloud_functions(12458):         at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
E/MethodChannel#cloud_functions(12458):         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:870)
E/flutter (12458): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: Exception: Unable to call function getUserData
E/flutter (12458): #0      HttpsCallable.call (package:cloud_functions/src/https_callable.dart:50:9)
E/flutter (12458): <asynchronous suspension>
E/flutter (12458): #1      _HomePageState._callable (package:firebaselogin/homepage.dart:160:35)
E/flutter (12458): <asynchronous suspension>
E/flutter (12458): #2      _HomePageState.build.<anonymous closure> (package:firebaselogin/homepage.dart:97:23)
E/flutter (12458): #3      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:779:19)
E/flutter (12458): #4      _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:862:36)
E/flutter (12458): #5      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24)
E/flutter (12458): #6      TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:504:11)
E/flutter (12458): #7      BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:282:5)
E/flutter (12458): #8      BaseTapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:217:7)
E/flutter (12458): #9      PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:475:9)
E/flutter (12458): #10     PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:76:12)
E/flutter (12458): #11     PointerRouter._dispatchEventToRoutes.<anonymous closure> (package:flutter/src/gestures/pointer_router.dart:122:9)
E/flutter (12458): #12     _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:379:8)
E/flutter (12458): #13     PointerRouter._dispatchEventToRoutes (package:flutter/src/gestures/pointer_router.dart:120:18)
E/flutter (12458): #14     PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:106:7)
E/flutter (12458): #15     GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:218:19)
E/flutter (12458): #16     GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:198:22)
E/flutter (12458): #17     GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:156:7)
E/flutter (12458): #18     GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:102:7)
E/flutter (12458): #19     GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:86:7)
E/flutter (12458): #20     _rootRunUnary (dart:async/zone.dart:1196:13)
E/flutter (12458): #21     _CustomZone.runUnary (dart:async/zone.dart:1085:19)
E/flutter (12458): #22     _CustomZone.runUnaryGuarded (dart:async/zone.dart:987:7)
E/flutter (12458): #23     _invoke1 (dart:ui/hooks.dart:275:10)
E/flutter (12458): #24     _dispatchPointerDataPacket (dart:ui/hooks.dart:184:5)

1 Ответ

2 голосов
/ 06 мая 2020

Вызываемые функции должны возвращать обещание, которое разрешается с данными для отправки клиенту. Прямо сейчас ваша функция вообще ничего не возвращает. Вероятно, вам следует вернуть обещание, возвращенное get().

exports.getUserData = functions.https.onCall((data) => {
    const test = data.text;
    return db.collection('users').doc(test).get();
});
...