我想接收在客户端传递的文档表单当前用户uid。我使用functions.onRequest进行了尝试,并成功地得到了结果,但在使用onCall方法时出现了错误。
云功能
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);
})
});
客户端呼叫
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)
可调用函数必须返回一个promise,该promise使用要发送到客户端的数据进行解析。现在,您的函数实际上根本没有返回任何内容。您可能应该返回get()
返回的承诺。
exports.getUserData = functions.https.onCall((data) => {
const test = data.text;
return db.collection('users').doc(test).get();
});