Cloud Functions for Firebase क्लाइंट SDK टूल की मदद से, Firebase ऐप्लिकेशन से सीधे फ़ंक्शन कॉल किए जा सकते हैं. इस तरह से अपने ऐप्लिकेशन से फ़ंक्शन कॉल करने के लिए, Cloud Functions में एचटीटीपी कॉल करने लायक फ़ंक्शन लिखें और उसे डिप्लॉय करें. इसके बाद, अपने ऐप्लिकेशन से फ़ंक्शन कॉल करने के लिए क्लाइंट लॉजिक जोड़ें.
यह ध्यान रखना ज़रूरी है कि एचटीटीपी कॉल किए जा सकने वाले फ़ंक्शन, एचटीटीपी फ़ंक्शन की तरह होते हैं, लेकिन उनसे अलग होते हैं. HTTP कॉल किए जा सकने वाले फ़ंक्शन का इस्तेमाल करने के लिए, आपको अपने प्लैटफ़ॉर्म के लिए क्लाइंट एसडीके का इस्तेमाल करना होगा. साथ ही, बैकएंड एपीआई का इस्तेमाल करना होगा या प्रोटोकॉल लागू करना होगा. कॉलेबल फ़ंक्शन और एचटीटीपी फ़ंक्शन में ये मुख्य अंतर होते हैं:
- कॉल करने लायक फ़ंक्शन के साथ, अनुरोधों में Firebase Authentication टोकन, FCM टोकन, और App Check टोकन अपने-आप शामिल हो जाते हैं. हालांकि, ऐसा तब होता है, जब ये टोकन उपलब्ध हों.
- ट्रिगर, अनुरोध के मुख्य हिस्से को अपने-आप डिसिरियलाइज़ करता है और पुष्टि करने वाले टोकन की पुष्टि करता है.
Firebase SDK, Cloud Functions दूसरी जनरेशन और इसके बाद के वर्शन के साथ काम करता है. साथ ही, HTTPS Callable फ़ंक्शन के साथ काम करने के लिए, Firebase क्लाइंट SDK के इन वर्शन के साथ काम करता है:
- Firebase Apple प्लैटफ़ॉर्म के लिए SDK टूल 12.0.0
- Firebase Android 21.2.1 के लिए एसडीके
- Firebase Modular Web SDK v. 9.7.0
अगर आपको किसी ऐसे ऐप्लिकेशन में मिलती-जुलती सुविधा जोड़नी है जिसे ऐसे प्लैटफ़ॉर्म पर बनाया गया है जिस पर https.onCall
काम नहीं करता है, तो https.onCall
के लिए प्रोटोकॉल स्पेसिफ़िकेशन देखें. इस गाइड के बाकी हिस्से में, Apple प्लैटफ़ॉर्म, Android, वेब, C++, और Unity के लिए, एचटीटीपी कॉल किए जा सकने वाले फ़ंक्शन को लिखने, डिप्लॉय करने, और कॉल करने के निर्देशों के बारे में बताया गया है.
कॉल किए जा सकने वाले फ़ंक्शन को लिखना और डिप्लॉय करना
एचटीटीपीएस कॉल करने योग्य फ़ंक्शन बनाने के लिए, functions.https.onCall
का इस्तेमाल करें. इस तरीके में दो पैरामीटर होते हैं: data
और वैकल्पिक context
:
// Saves a message to the Firebase Realtime Database but sanitizes the // text by removing swearwords. exports.addMessage = functions.https.onCall((data, context) => { // ... });
कॉल किए जा सकने वाले ऐसे फ़ंक्शन के लिए जो Realtime Database में टेक्स्ट मैसेज सेव करता है, उदाहरण के लिए, data
में मैसेज का टेक्स्ट हो सकता है. वहीं, context
पैरामीटर में उपयोगकर्ता की पुष्टि करने से जुड़ी जानकारी होती है:
// Message text passed from the client.
const text = request.data.text;
// Authentication / user information is automatically added to the request.
const uid = request.auth.uid;
const name = request.auth.token.name || null;
const picture = request.auth.token.picture || null;
const email = request.auth.token.email || null;
कॉल किए जा सकने वाले फ़ंक्शन की जगह और कॉल करने वाले क्लाइंट की जगह के बीच की दूरी की वजह से, नेटवर्क इंतज़ार का समय बढ़ सकता है. परफ़ॉर्मेंस को ऑप्टिमाइज़ करने के लिए, जहां लागू हो वहां फ़ंक्शन की जगह तय करें. साथ ही, पक्का करें कि कॉल किए जा सकने वाले फ़ंक्शन की जगह, क्लाइंट-साइड पर एसडीके टूल शुरू करते समय सेट की गई जगह के मुताबिक हो.
आपके पास App Check अटेस्टेशन अटैच करने का विकल्प होता है. इससे, बिलिंग में धोखाधड़ी या फ़िशिंग जैसे गलत इस्तेमाल से अपने बैकएंड संसाधनों को सुरक्षित रखने में मदद मिलती है. Cloud Functions के लिए, App Check एनफ़ोर्समेंट चालू करें लेख पढ़ें.
नतीजा वापस भेजा जा रहा है
क्लाइंट को डेटा वापस भेजने के लिए, ऐसा डेटा वापस भेजें जिसे JSON में कोड में बदला जा सकता हो. उदाहरण के लिए, जोड़ की कार्रवाई का नतीजा पाने के लिए:
// returning result.
return {
firstNumber: firstNumber,
secondNumber: secondNumber,
operator: "+",
operationResult: firstNumber + secondNumber,
};
एसिंक्रोनस ऑपरेशन के बाद डेटा वापस पाने के लिए, प्रॉमिस को वापस पाएं. प्रॉमिस से मिला डेटा, क्लाइंट को वापस भेज दिया जाता है. उदाहरण के लिए, आपके पास सैनिटाइज़ किया गया ऐसा टेक्स्ट दिखाने का विकल्प होता है जिसे कॉल किए जा सकने वाले फ़ंक्शन ने Realtime Database में लिखा है:
// Saving the new message to the Realtime Database.
const sanitizedMessage = sanitizer.sanitizeText(text); // Sanitize message.
return getDatabase().ref("/messages").push({
text: sanitizedMessage,
author: {uid, name, picture, email},
}).then(() => {
logger.info("New Message written");
// Returning the sanitized message to the client.
return {text: sanitizedMessage};
})
गड़बड़ियां ठीक करना
यह पक्का करने के लिए कि क्लाइंट को गड़बड़ी की काम की जानकारी मिले, कॉल किए जा सकने वाले फ़ंक्शन से गड़बड़ियां वापस पाएं. इसके लिए, functions.https.HttpsError
के इंस्टेंस को थ्रो करें या अस्वीकार किए गए प्रॉमिस को वापस भेजें.
गड़बड़ी में code
एट्रिब्यूट होता है. इसकी वैल्यू, functions.https.HttpsError
पर दी गई वैल्यू में से कोई एक हो सकती है.
गड़बड़ियों में एक स्ट्रिंग message
भी होती है, जो डिफ़ॉल्ट रूप से एक खाली स्ट्रिंग होती है. इनमें details
फ़ील्ड भी हो सकता है. हालांकि, यह ज़रूरी नहीं है. इसमें कोई भी वैल्यू डाली जा सकती है. अगर आपके फ़ंक्शन से HttpsError
के अलावा कोई और गड़बड़ी होती है, तो आपके क्लाइंट को INTERNAL
मैसेज और internal
कोड के साथ गड़बड़ी का मैसेज मिलता है.
उदाहरण के लिए, कोई फ़ंक्शन डेटा की पुष्टि करने और पुष्टि करने से जुड़ी गड़बड़ियां दिखा सकता है. साथ ही, गड़बड़ी के मैसेज के साथ कॉलिंग क्लाइंट को वापस भेज सकता है:
// Checking attribute.
if (!(typeof text === "string") || text.length === 0) {
// Throwing an HttpsError so that the client gets the error details.
throw new HttpsError("invalid-argument", "The function must be called " +
"with one arguments \"text\" containing the message text to add.");
}
// Checking that the user is authenticated.
if (!request.auth) {
// Throwing an HttpsError so that the client gets the error details.
throw new HttpsError("failed-precondition", "The function must be " +
"called while authenticated.");
}
कॉल किए जा सकने वाले फ़ंक्शन को डिप्लॉय करना
index.js
में कॉल किए जा सकने वाले फ़ंक्शन को सेव करने के बाद, firebase deploy
चलाने पर इसे अन्य सभी फ़ंक्शन के साथ डिप्लॉय किया जाता है.
सिर्फ़ कॉल किए जा सकने वाले फ़ंक्शन को डिप्लॉय करने के लिए, --only
आर्ग्युमेंट का इस्तेमाल करें. इससे आंशिक डिप्लॉयमेंट किए जा सकते हैं:
firebase deploy --only functions:addMessage
अगर फ़ंक्शन डिप्लॉय करते समय अनुमतियों से जुड़ी गड़बड़ियां आती हैं, तो पक्का करें कि डिप्लॉयमेंट कमांड चलाने वाले उपयोगकर्ता को सही IAM भूमिकाएं असाइन की गई हों.
क्लाइंट डेवलपमेंट एनवायरमेंट सेट अप करना
पक्का करें कि आपने सभी ज़रूरी शर्तें पूरी कर ली हों. इसके बाद, अपने ऐप्लिकेशन में ज़रूरी डिपेंडेंसी और क्लाइंट लाइब्रेरी जोड़ें.
iOS+
अपने Apple ऐप्लिकेशन में Firebase जोड़ने के लिए, निर्देशों का पालन करें.
Firebase डिपेंडेंसी इंस्टॉल और मैनेज करने के लिए, Swift Package Manager का इस्तेमाल करें.
- Xcode में, अपने ऐप्लिकेशन प्रोजेक्ट को खोलें. इसके बाद, File > Add Packages पर जाएं.
- जब आपसे कहा जाए, तब Firebase Apple प्लैटफ़ॉर्म SDK टूल की रिपॉज़िटरी जोड़ें:
- Cloud Functions लाइब्रेरी चुनें.
- टारगेट की बिल्ड सेटिंग के Other Linker Flags सेक्शन में
-ObjC
फ़्लैग जोड़ें. - इसके बाद, Xcode आपके पैकेज की डिपेंडेंसी से जुड़ी समस्या को हल करना शुरू कर देगा और उन्हें बैकग्राउंड में डाउनलोड करेगा.
https://github.com/firebase/firebase-ios-sdk.git
Web
- अपने वेब ऐप्लिकेशन में Firebase जोड़ने के लिए, निर्देशों का पालन करें. पक्का करें कि आपने अपने टर्मिनल से यह कमांड चलाई हो:
npm install firebase@12.0.0 --save
Firebase Core और Cloud Functions, दोनों को मैन्युअल तरीके से चालू करें:
import { initializeApp } from 'firebase/app'; import { getFunctions } from 'firebase/functions'; const app = initializeApp({ projectId: '### CLOUD FUNCTIONS PROJECT ID ###', apiKey: '### FIREBASE API KEY ###', authDomain: '### FIREBASE AUTH DOMAIN ###', }); const functions = getFunctions(app);
Web
- अपने वेब ऐप्लिकेशन में Firebase जोड़ने के लिए, निर्देशों का पालन करें.
- अपने ऐप्लिकेशन में Firebase कोर और Cloud Functions क्लाइंट लाइब्रेरी जोड़ें:
<script src="https://www.gstatic.com/firebasejs/8.10.1/firebase.js"></script> <script src="https://www.gstatic.com/firebasejs/8.10.1/firebase-functions.js"></script>
Cloud Functions SDK टूल, npm पैकेज के तौर पर भी उपलब्ध है.
- अपने टर्मिनल में यह कमांड चलाएं:
npm install firebase@8.10.1 --save
- Firebase Core और Cloud Functions, दोनों को मैन्युअल तरीके से चालू करें:
const firebase = require("firebase"); // Required for side-effects require("firebase/functions");
Kotlin
अपने Android ऐप्लिकेशन में Firebase जोड़ने के लिए, निर्देशों का पालन करें.
अपनी मॉड्यूल (ऐप्लिकेशन-लेवल) की Gradle फ़ाइल (आम तौर पर
<project>/<app-module>/build.gradle.kts
या<project>/<app-module>/build.gradle
) में, Android के लिए Cloud Functions लाइब्रेरी की डिपेंडेंसी जोड़ें. हमारा सुझाव है कि लाइब्रेरी के वर्शन को कंट्रोल करने के लिए, Firebase Android BoM का इस्तेमाल करें.dependencies { // Import the BoM for the Firebase platform implementation(platform("com.google.firebase:firebase-bom:33.16.0")) // Add the dependency for the Cloud Functions library // When using the BoM, you don't specify versions in Firebase library dependencies implementation("com.google.firebase:firebase-functions") }
Firebase Android BoM का इस्तेमाल करने पर, आपका ऐप्लिकेशन हमेशा Firebase Android लाइब्रेरी के साथ काम करने वाले वर्शन का इस्तेमाल करेगा.
(वैकल्पिक) BoM का इस्तेमाल किए बिना, Firebase लाइब्रेरी की डिपेंडेंसी जोड़ें
अगर आपको Firebase BoM का इस्तेमाल नहीं करना है, तो आपको डिपेंडेंसी लाइन में Firebase की हर लाइब्रेरी के वर्शन की जानकारी देनी होगी.
ध्यान दें कि अगर आपके ऐप्लिकेशन में Firebase की एक से ज़्यादा लाइब्रेरी का इस्तेमाल किया जाता है, तो हमारा सुझाव है कि लाइब्रेरी के वर्शन मैनेज करने के लिए BoM का इस्तेमाल करें. इससे यह पक्का किया जा सकता है कि सभी वर्शन एक-दूसरे के साथ काम करते हों.
dependencies { // Add the dependency for the Cloud Functions library // When NOT using the BoM, you must specify versions in Firebase library dependencies implementation("com.google.firebase:firebase-functions:21.2.1") }
Java
अपने Android ऐप्लिकेशन में Firebase जोड़ने के लिए, निर्देशों का पालन करें.
अपनी मॉड्यूल (ऐप्लिकेशन-लेवल) की Gradle फ़ाइल (आम तौर पर
<project>/<app-module>/build.gradle.kts
या<project>/<app-module>/build.gradle
) में, Android के लिए Cloud Functions लाइब्रेरी की डिपेंडेंसी जोड़ें. हमारा सुझाव है कि लाइब्रेरी के वर्शन को कंट्रोल करने के लिए, Firebase Android BoM का इस्तेमाल करें.dependencies { // Import the BoM for the Firebase platform implementation(platform("com.google.firebase:firebase-bom:33.16.0")) // Add the dependency for the Cloud Functions library // When using the BoM, you don't specify versions in Firebase library dependencies implementation("com.google.firebase:firebase-functions") }
Firebase Android BoM का इस्तेमाल करने पर, आपका ऐप्लिकेशन हमेशा Firebase Android लाइब्रेरी के साथ काम करने वाले वर्शन का इस्तेमाल करेगा.
(वैकल्पिक) BoM का इस्तेमाल किए बिना, Firebase लाइब्रेरी की डिपेंडेंसी जोड़ें
अगर आपको Firebase BoM का इस्तेमाल नहीं करना है, तो आपको डिपेंडेंसी लाइन में Firebase की हर लाइब्रेरी के वर्शन की जानकारी देनी होगी.
ध्यान दें कि अगर आपके ऐप्लिकेशन में Firebase की एक से ज़्यादा लाइब्रेरी का इस्तेमाल किया जाता है, तो हमारा सुझाव है कि लाइब्रेरी के वर्शन मैनेज करने के लिए BoM का इस्तेमाल करें. इससे यह पक्का किया जा सकता है कि सभी वर्शन एक-दूसरे के साथ काम करते हों.
dependencies { // Add the dependency for the Cloud Functions library // When NOT using the BoM, you must specify versions in Firebase library dependencies implementation("com.google.firebase:firebase-functions:21.2.1") }
Dart
अपने Flutter ऐप्लिकेशन में Firebase जोड़ने के लिए, निर्देशों का पालन करें.
प्लगिन इंस्टॉल करने के लिए, अपने Flutter प्रोजेक्ट के रूट से यह कमांड चलाएं:
flutter pub add cloud_functions
अपडेट हो जाने के बाद, अपने Flutter ऐप्लिकेशन को फिर से बनाएं:
flutter run
इंस्टॉल हो जाने के बाद,
cloud_functions
प्लगिन को ऐक्सेस किया जा सकता है. इसके लिए, इसे अपने डार्ट कोड में इंपोर्ट करें:import 'package:cloud_functions/cloud_functions.dart';
C++
Android के साथ C++ के लिए:
- अपने C++ प्रोजेक्ट को Firebase से जोड़ने के लिए, निर्देशों का पालन करें.
firebase_functions
लाइब्रेरी को अपनीCMakeLists.txt
फ़ाइल में जोड़ें.
Apple प्लैटफ़ॉर्म के साथ C++ के लिए:
- अपने C++ प्रोजेक्ट को Firebase से जोड़ने के लिए, निर्देशों का पालन करें.
- अपने
Podfile
में Cloud Functions पॉड जोड़ें:pod 'Firebase/Functions'
- फ़ाइल सेव करें. इसके बाद, यह कमांड चलाएं:
pod install
- अपने Xcode प्रोजेक्ट में, Firebase C++ SDK से Firebase कोर और Cloud Functions फ़्रेमवर्क जोड़ें.
firebase.framework
firebase_functions.framework
Unity
- अपने Unity प्रोजेक्ट में Firebase जोड़ने के लिए, निर्देशों का पालन करें.
- अपने Unity प्रोजेक्ट में, Firebase Unity SDK से
FirebaseFunctions.unitypackage
जोड़ें.
क्लाइंट SDK टूल को शुरू करना
Cloud Functions का कोई इंस्टेंस शुरू करें:
Swift
lazy var functions = Functions.functions()
Objective-C
@property(strong, nonatomic) FIRFunctions *functions;
// ...
self.functions = [FIRFunctions functions];
Web
firebase.initializeApp({
apiKey: '### FIREBASE API KEY ###',
authDomain: '### FIREBASE AUTH DOMAIN ###',
projectId: '### CLOUD FUNCTIONS PROJECT ID ###'
databaseURL: 'https://### YOUR DATABASE NAME ###.firebaseio.com',
});
// Initialize Cloud Functions through Firebase
var functions = firebase.functions();
Web
const app = initializeApp({
projectId: '### CLOUD FUNCTIONS PROJECT ID ###',
apiKey: '### FIREBASE API KEY ###',
authDomain: '### FIREBASE AUTH DOMAIN ###',
});
const functions = getFunctions(app);
Kotlin
private lateinit var functions: FirebaseFunctions // ... functions = Firebase.functions
Java
private FirebaseFunctions mFunctions; // ... mFunctions = FirebaseFunctions.getInstance();
Dart
final functions = FirebaseFunctions.instance;
C++
firebase::functions::Functions* functions;
// ...
functions = firebase::functions::Functions::GetInstance(app);
Unity
functions = Firebase.Functions.DefaultInstance;
फ़ंक्शन को कॉल करना
Swift
functions.httpsCallable("addMessage").call(["text": inputField.text]) { result, error in
if let error = error as NSError? {
if error.domain == FunctionsErrorDomain {
let code = FunctionsErrorCode(rawValue: error.code)
let message = error.localizedDescription
let details = error.userInfo[FunctionsErrorDetailsKey]
}
// ...
}
if let data = result?.data as? [String: Any], let text = data["text"] as? String {
self.resultField.text = text
}
}
Objective-C
[[_functions HTTPSCallableWithName:@"addMessage"] callWithObject:@{@"text": _inputField.text}
completion:^(FIRHTTPSCallableResult * _Nullable result, NSError * _Nullable error) {
if (error) {
if ([error.domain isEqual:@"com.firebase.functions"]) {
FIRFunctionsErrorCode code = error.code;
NSString *message = error.localizedDescription;
NSObject *details = error.userInfo[@"details"];
}
// ...
}
self->_resultField.text = result.data[@"text"];
}];
Web
var addMessage = firebase.functions().httpsCallable('addMessage');
addMessage({ text: messageText })
.then((result) => {
// Read result of the Cloud Function.
var sanitizedMessage = result.data.text;
});
Web
import { getFunctions, httpsCallable } from "firebase/functions";
const functions = getFunctions();
const addMessage = httpsCallable(functions, 'addMessage');
addMessage({ text: messageText })
.then((result) => {
// Read result of the Cloud Function.
/** @type {any} */
const data = result.data;
const sanitizedMessage = data.text;
});
Kotlin
private fun addMessage(text: String): Task<String> { // Create the arguments to the callable function. val data = hashMapOf( "text" to text, "push" to true, ) return functions .getHttpsCallable("addMessage") .call(data) .continueWith { task -> // This continuation runs on either success or failure, but if the task // has failed then result will throw an Exception which will be // propagated down. val result = task.result?.data as String result } }
Java
private Task<String> addMessage(String text) { // Create the arguments to the callable function. Map<String, Object> data = new HashMap<>(); data.put("text", text); data.put("push", true); return mFunctions .getHttpsCallable("addMessage") .call(data) .continueWith(new Continuation<HttpsCallableResult, String>() { @Override public String then(@NonNull Task<HttpsCallableResult> task) throws Exception { // This continuation runs on either success or failure, but if the task // has failed then getResult() will throw an Exception which will be // propagated down. String result = (String) task.getResult().getData(); return result; } }); }
Dart
final result = await FirebaseFunctions.instance.httpsCallable('addMessage').call(
{
"text": text,
"push": true,
},
);
_response = result.data as String;
C++
firebase::Future<firebase::functions::HttpsCallableResult> AddMessage(
const std::string& text) {
// Create the arguments to the callable function.
firebase::Variant data = firebase::Variant::EmptyMap();
data.map()["text"] = firebase::Variant(text);
data.map()["push"] = true;
// Call the function and add a callback for the result.
firebase::functions::HttpsCallableReference doSomething =
functions->GetHttpsCallable("addMessage");
return doSomething.Call(data);
}
Unity
private Task<string> addMessage(string text) {
// Create the arguments to the callable function.
var data = new Dictionary<string, object>();
data["text"] = text;
data["push"] = true;
// Call the function and extract the operation from the result.
var function = functions.GetHttpsCallable("addMessage");
return function.CallAsync(data).ContinueWith((task) => {
return (string) task.Result.Data;
});
}
क्लाइंट पर गड़बड़ियों को ठीक करना
अगर सर्वर में कोई गड़बड़ी हुई है या प्रॉमिस अस्वीकार कर दिया गया है, तो क्लाइंट को गड़बड़ी का मैसेज मिलता है.
अगर फ़ंक्शन से मिली गड़बड़ी function.https.HttpsError
टाइप की है, तो क्लाइंट को सर्वर से मिली गड़बड़ी के तौर पर code
, message
, और details
गड़बड़ी मिलती है. अगर ऐसा नहीं होता है, तो गड़बड़ी के मैसेज में INTERNAL
और कोड INTERNAL
शामिल होता है. कॉल किए जा सकने वाले फ़ंक्शन में गड़बड़ियों को ठीक करने के बारे में दिशा-निर्देश देखें.
Swift
if let error = error as NSError? {
if error.domain == FunctionsErrorDomain {
let code = FunctionsErrorCode(rawValue: error.code)
let message = error.localizedDescription
let details = error.userInfo[FunctionsErrorDetailsKey]
}
// ...
}
Objective-C
if (error) {
if ([error.domain isEqual:@"com.firebase.functions"]) {
FIRFunctionsErrorCode code = error.code;
NSString *message = error.localizedDescription;
NSObject *details = error.userInfo[@"details"];
}
// ...
}
Web
var addMessage = firebase.functions().httpsCallable('addMessage');
addMessage({ text: messageText })
.then((result) => {
// Read result of the Cloud Function.
var sanitizedMessage = result.data.text;
})
.catch((error) => {
// Getting the Error details.
var code = error.code;
var message = error.message;
var details = error.details;
// ...
});
Web
import { getFunctions, httpsCallable } from "firebase/functions";
const functions = getFunctions();
const addMessage = httpsCallable(functions, 'addMessage');
addMessage({ text: messageText })
.then((result) => {
// Read result of the Cloud Function.
/** @type {any} */
const data = result.data;
const sanitizedMessage = data.text;
})
.catch((error) => {
// Getting the Error details.
const code = error.code;
const message = error.message;
const details = error.details;
// ...
});
Kotlin
addMessage(inputMessage) .addOnCompleteListener { task -> if (!task.isSuccessful) { val e = task.exception if (e is FirebaseFunctionsException) { val code = e.code val details = e.details } } }
Java
addMessage(inputMessage) .addOnCompleteListener(new OnCompleteListener<String>() { @Override public void onComplete(@NonNull Task<String> task) { if (!task.isSuccessful()) { Exception e = task.getException(); if (e instanceof FirebaseFunctionsException) { FirebaseFunctionsException ffe = (FirebaseFunctionsException) e; FirebaseFunctionsException.Code code = ffe.getCode(); Object details = ffe.getDetails(); } } } });
Dart
try {
final result =
await FirebaseFunctions.instance.httpsCallable('addMessage').call();
} on FirebaseFunctionsException catch (error) {
print(error.code);
print(error.details);
print(error.message);
}
C++
void OnAddMessageCallback(
const firebase::Future<firebase::functions::HttpsCallableResult>& future) {
if (future.error() != firebase::functions::kErrorNone) {
// Function error code, will be kErrorInternal if the failure was not
// handled properly in the function call.
auto code = static_cast<firebase::functions::Error>(future.error());
// Display the error in the UI.
DisplayError(code, future.error_message());
return;
}
const firebase::functions::HttpsCallableResult* result = future.result();
firebase::Variant data = result->data();
// This will assert if the result returned from the function wasn't a string.
std::string message = data.string_value();
// Display the result in the UI.
DisplayResult(message);
}
// ...
// ...
auto future = AddMessage(message);
future.OnCompletion(OnAddMessageCallback);
// ...
Unity
addMessage(text).ContinueWith((task) => {
if (task.IsFaulted) {
foreach (var inner in task.Exception.InnerExceptions) {
if (inner is FunctionsException) {
var e = (FunctionsException) inner;
// Function error code, will be INTERNAL if the failure
// was not handled properly in the function call.
var code = e.ErrorCode;
var message = e.ErrorMessage;
}
}
} else {
string result = task.Result;
}
});
सुझाया गया: App Check की मदद से गलत इस्तेमाल को रोकना
अपना ऐप्लिकेशन लॉन्च करने से पहले, आपको App Check चालू करना चाहिए, ताकि यह पक्का किया जा सके कि सिर्फ़ आपके ऐप्लिकेशन, कॉल किए जा सकने वाले फ़ंक्शन के एंडपॉइंट को ऐक्सेस कर सकें.