![](/img/trans.png)
[英]Firebase Callable Cloud Function CORS Error using Firebase Emulator and Vite
[英]Callable Cloud Function error: Response is missing data field
不知道如何從 flutter 中的雲 function 獲得響應。
我的雲 function
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.testDemo = functions.https.onRequest((request, response) => {
return response.status(200).json({msg:"Hello from Firebase!"});
});
我的 flutter 密碼
///Getting an instance of the callable function:
try {
final HttpsCallable callable = CloudFunctions.instance.getHttpsCallable(
functionName: 'testDemo',);
///Calling the function with parameters:
dynamic resp = await callable.call();
print("this is responce from firebase $resp");
} on CloudFunctionsException catch (e) {
print('caught firebase functions exception');
print(e.code);
print(e.message);
print(e.details);
} catch (e) {
print('caught generic exception');
print(e);
}
flutter:捕獲 firebase 函數異常 flutter:內部 flutter:響應缺少數據字段。 flutter: null
利用
exports.testDemo = functions.https.onCall((data, context) => {
return {msg:"Hello from Firebase!"};
});
調用函數時需要添加參數:
改變:
// Calling a function without parameters is a different function!
dynamic resp = await callable.call();
至:
dynamic resp = await callable.call(
<String, dynamic>{
'YOUR_PARAMETER_NAME': 'YOUR_PARAMETER_VALUE',
},
);
如此處所述
然后打印響應:
print(resp.data)
print(resp.data['msg'])
您必須明確地將屬性“數據”放在響應的 json 中。
喜歡:
response.send({
"status" : success,
"data" : "some... data"
});
如果你想通過它的名字調用 function,你可以轉換為.onCall 類型 function。 aldobaie 的回答
或者對於 onRequest 類型 function:
我們需要將其稱為 RESTful API。
在部署到 firebase 時,我們將獲得函數 URL
或者我們可以從 firebase 的功能儀表板中獲取 function URL。
URL 格式為:
https://us-central1-<項目名>.cloudfunctions.net/<函數名>
將它稱為任何其他 RESTful API。加上將它用作 API 的一點是響應中的數據字段不是強制性的並且響應格式自由。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.