फायरबेस क्लाइंट एसडीके के लिए क्लाउड फ़ंक्शंस आपको सीधे फायरबेस ऐप से फ़ंक्शन कॉल करने देते हैं। इस तरह से अपने ऐप से किसी फ़ंक्शन को कॉल करने के लिए, क्लाउड फ़ंक्शंस में एक HTTP कॉल करने योग्य फ़ंक्शन लिखें और तैनात करें, और फिर अपने ऐप से फ़ंक्शन को कॉल करने के लिए क्लाइंट लॉजिक जोड़ें।
यह ध्यान रखना महत्वपूर्ण है कि HTTP कॉल करने योग्य फ़ंक्शन समान हैं लेकिन HTTP फ़ंक्शन के समान नहीं हैं । HTTP कॉल करने योग्य फ़ंक्शंस का उपयोग करने के लिए आपको बैकएंड एपीआई के साथ अपने प्लेटफ़ॉर्म के लिए क्लाइंट एसडीके का उपयोग करना होगा (या प्रोटोकॉल लागू करना होगा)। कॉलेबल्स में HTTP फ़ंक्शंस से ये मुख्य अंतर हैं:
- कॉलेबल्स के साथ, फायरबेस प्रमाणीकरण टोकन, एफसीएम टोकन और ऐप चेक टोकन, उपलब्ध होने पर, स्वचालित रूप से अनुरोधों में शामिल हो जाते हैं।
- ट्रिगर स्वचालित रूप से अनुरोध निकाय को डीसेरिएलाइज़ करता है और ऑथ टोकन को मान्य करता है।
क्लाउड फ़ंक्शंस के लिए फ़ायरबेस एसडीके दूसरी पीढ़ी और उच्चतर HTTPS कॉल करने योग्य फ़ंक्शंस का समर्थन करने के लिए इन फ़ायरबेस क्लाइंट एसडीके न्यूनतम संस्करणों के साथ इंटरऑपरेट करता है:
- एप्पल प्लेटफॉर्म के लिए फायरबेस एसडीके 10.18.0
- एंड्रॉइड 20.4.0 के लिए फायरबेस एसडीके
- फायरबेस मॉड्यूलर वेब एसडीके वी. 9.7.0
यदि आप किसी असमर्थित प्लेटफ़ॉर्म पर बने ऐप में समान कार्यक्षमता जोड़ना चाहते हैं, तो https.onCall
के लिए प्रोटोकॉल विशिष्टता देखें। इस मार्गदर्शिका का शेष भाग Apple प्लेटफ़ॉर्म, Android, वेब, C++ और यूनिटी के लिए HTTP कॉल करने योग्य फ़ंक्शन को लिखने, तैनात करने और कॉल करने के तरीके के बारे में निर्देश प्रदान करता है।
कॉल करने योग्य फ़ंक्शन लिखें और तैनात करें
HTTPS कॉल करने योग्य फ़ंक्शन बनाने के लिए 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) => {
// ...
});
एक कॉल करने योग्य फ़ंक्शन के लिए जो एक टेक्स्ट संदेश को रीयलटाइम डेटाबेस में सहेजता है, उदाहरण के लिए, data
में संदेश टेक्स्ट हो सकता है, जबकि context
पैरामीटर उपयोगकर्ता की जानकारी का प्रतिनिधित्व करते हैं:
// Message text passed from the client.
const text = data.text;
// Authentication / user information is automatically added to the request.
const uid = context.auth.uid;
const name = context.auth.token.name || null;
const picture = context.auth.token.picture || null;
const email = context.auth.token.email || null;
कॉल करने योग्य फ़ंक्शन के स्थान और कॉलिंग क्लाइंट के स्थान के बीच की दूरी नेटवर्क विलंबता पैदा कर सकती है। प्रदर्शन को अनुकूलित करने के लिए, जहां लागू हो वहां फ़ंक्शन स्थान निर्दिष्ट करने पर विचार करें, और जब आप क्लाइंट साइड पर एसडीके प्रारंभ करते हैं तो कॉल करने योग्य स्थान को स्थान सेट के साथ संरेखित करना सुनिश्चित करें।
वैकल्पिक रूप से, आप अपने बैकएंड संसाधनों को बिलिंग धोखाधड़ी या फ़िशिंग जैसे दुरुपयोग से बचाने में मदद के लिए एक ऐप चेक सत्यापन संलग्न कर सकते हैं। क्लाउड फ़ंक्शंस के लिए ऐप चेक प्रवर्तन सक्षम करें देखें।
परिणाम वापस भेजा जा रहा है
क्लाइंट को डेटा वापस भेजने के लिए, वह डेटा लौटाएँ जिसे JSON एनकोड किया जा सकता है। उदाहरण के लिए, किसी अतिरिक्त ऑपरेशन का परिणाम वापस करने के लिए:
// returning result.
return {
firstNumber: firstNumber,
secondNumber: secondNumber,
operator: '+',
operationResult: firstNumber + secondNumber,
};
एसिंक्रोनस ऑपरेशन के बाद डेटा वापस करने के लिए, एक वादा लौटाएँ। वादे द्वारा लौटाया गया डेटा क्लाइंट को वापस भेज दिया जाता है। उदाहरण के लिए, आप कॉल करने योग्य फ़ंक्शन द्वारा रीयलटाइम डेटाबेस में लिखे गए स्वच्छ पाठ को वापस कर सकते हैं:
// Saving the new message to the Realtime Database.
const sanitizedMessage = sanitizer.sanitizeText(text); // Sanitize the message.
return admin.database().ref('/messages').push({
text: sanitizedMessage,
author: { uid, name, picture, email },
}).then(() => {
console.log('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 functions.https.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 (!context.auth) {
// Throwing an HttpsError so that the client gets the error details.
throw new functions.https.HttpsError('failed-precondition', 'The function must be called ' +
'while authenticated.');
}
कॉल करने योग्य फ़ंक्शन को तैनात करें
आपके द्वारा एक पूर्ण कॉल करने योग्य फ़ंक्शन को index.js
के भीतर सहेजने के बाद, जब आप firebase deploy
चलाते हैं तो इसे अन्य सभी कार्यों के साथ तैनात किया जाता है। केवल कॉल करने योग्य को तैनात करने के लिए, --only
तर्क का उपयोग करें जैसा कि आंशिक तैनाती करने के लिए दिखाया गया है:
firebase deploy --only functions:addMessage
यदि आप फ़ंक्शंस को तैनात करते समय अनुमति त्रुटियों का सामना करते हैं, तो सुनिश्चित करें कि परिनियोजन आदेश चलाने वाले उपयोगकर्ता को उचित IAM भूमिकाएँ सौंपी गई हैं।
अपना ग्राहक विकास परिवेश स्थापित करें
सुनिश्चित करें कि आप सभी आवश्यक शर्तें पूरी करते हैं, फिर अपने ऐप में आवश्यक निर्भरताएँ और क्लाइंट लाइब्रेरी जोड़ें।
आईओएस+
अपने Apple ऐप में Firebase जोड़ने के लिए निर्देशों का पालन करें।
फायरबेस निर्भरता को स्थापित और प्रबंधित करने के लिए स्विफ्ट पैकेज मैनेजर का उपयोग करें।
- Xcode में, अपना ऐप प्रोजेक्ट खुला होने पर, फ़ाइल > पैकेज जोड़ें पर नेविगेट करें।
- संकेत मिलने पर, Firebase Apple प्लेटफ़ॉर्म SDK रिपॉजिटरी जोड़ें:
- क्लाउड फ़ंक्शंस लाइब्रेरी चुनें।
- अपने लक्ष्य की बिल्ड सेटिंग्स के अन्य लिंकर फ़्लैग अनुभाग में
-ObjC
फ़्लैग जोड़ें। - समाप्त होने पर, Xcode स्वचालित रूप से पृष्ठभूमि में आपकी निर्भरता को हल करना और डाउनलोड करना शुरू कर देगा।
https://github.com/firebase/firebase-ios-sdk.git
वेब मॉड्यूलर एपीआई
- अपने वेब ऐप में फायरबेस जोड़ने के लिए निर्देशों का पालन करें। अपने टर्मिनल से निम्नलिखित कमांड चलाना सुनिश्चित करें:
npm install firebase@10.7.0 --save
मैन्युअल रूप से फायरबेस कोर और क्लाउड फ़ंक्शंस दोनों की आवश्यकता होती है:
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);
वेब नेमस्पेस्ड एपीआई
- अपने वेब ऐप में फायरबेस जोड़ने के लिए निर्देशों का पालन करें।
- अपने ऐप में फायरबेस कोर और क्लाउड फ़ंक्शंस क्लाइंट लाइब्रेरी जोड़ें:
<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>
क्लाउड फ़ंक्शंस एसडीके एनपीएम पैकेज के रूप में भी उपलब्ध है।
- अपने टर्मिनल से निम्नलिखित कमांड चलाएँ:
npm install firebase@8.10.1 --save
- मैन्युअल रूप से फायरबेस कोर और क्लाउड फ़ंक्शंस दोनों की आवश्यकता होती है:
const firebase = require("firebase"); // Required for side-effects require("firebase/functions");
Kotlin+KTX
अपने एंड्रॉइड ऐप में फायरबेस जोड़ने के लिए निर्देशों का पालन करें।
अपने मॉड्यूल (ऐप-स्तर) ग्रैडल फ़ाइल में (आमतौर पर
<project>/<app-module>/build.gradle.kts
या<project>/<app-module>/build.gradle
), क्लाउड फ़ंक्शंस के लिए निर्भरता जोड़ें Android के लिए लाइब्रेरी. हम लाइब्रेरी वर्जनिंग को नियंत्रित करने के लिए फायरबेस एंड्रॉइड BoM का उपयोग करने की सलाह देते हैं।dependencies { // Import the BoM for the Firebase platform implementation(platform("com.google.firebase:firebase-bom:32.6.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") }
फायरबेस एंड्रॉइड बीओएम का उपयोग करके, आपका ऐप हमेशा फायरबेस एंड्रॉइड लाइब्रेरी के संगत संस्करणों का उपयोग करेगा।
कोटलिन-विशिष्ट लाइब्रेरी मॉड्यूल खोज रहे हैं? अक्टूबर 2023 (फायरबेस बीओएम 32.5.0) से शुरू होकर, कोटलिन और जावा डेवलपर्स दोनों मुख्य लाइब्रेरी मॉड्यूल पर निर्भर हो सकते हैं (विवरण के लिए, इस पहल के बारे में अक्सर पूछे जाने वाले प्रश्न देखें)।(वैकल्पिक) BoM का उपयोग किए बिना फायरबेस लाइब्रेरी निर्भरताएँ जोड़ें
यदि आप फायरबेस बीओएम का उपयोग नहीं करना चुनते हैं, तो आपको प्रत्येक फायरबेस लाइब्रेरी संस्करण को उसकी निर्भरता पंक्ति में निर्दिष्ट करना होगा।
ध्यान दें कि यदि आप अपने ऐप में एकाधिक फायरबेस लाइब्रेरी का उपयोग करते हैं, तो हम लाइब्रेरी संस्करणों को प्रबंधित करने के लिए 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:20.4.0") }
Java
अपने एंड्रॉइड ऐप में फायरबेस जोड़ने के लिए निर्देशों का पालन करें।
अपने मॉड्यूल (ऐप-स्तर) ग्रैडल फ़ाइल में (आमतौर पर
<project>/<app-module>/build.gradle.kts
या<project>/<app-module>/build.gradle
), क्लाउड फ़ंक्शंस के लिए निर्भरता जोड़ें Android के लिए लाइब्रेरी. हम लाइब्रेरी वर्जनिंग को नियंत्रित करने के लिए फायरबेस एंड्रॉइड BoM का उपयोग करने की सलाह देते हैं।dependencies { // Import the BoM for the Firebase platform implementation(platform("com.google.firebase:firebase-bom:32.6.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") }
फायरबेस एंड्रॉइड बीओएम का उपयोग करके, आपका ऐप हमेशा फायरबेस एंड्रॉइड लाइब्रेरी के संगत संस्करणों का उपयोग करेगा।
कोटलिन-विशिष्ट लाइब्रेरी मॉड्यूल खोज रहे हैं? अक्टूबर 2023 (फायरबेस बीओएम 32.5.0) से शुरू होकर, कोटलिन और जावा डेवलपर्स दोनों मुख्य लाइब्रेरी मॉड्यूल पर निर्भर हो सकते हैं (विवरण के लिए, इस पहल के बारे में अक्सर पूछे जाने वाले प्रश्न देखें)।(वैकल्पिक) BoM का उपयोग किए बिना फायरबेस लाइब्रेरी निर्भरताएँ जोड़ें
यदि आप फायरबेस बीओएम का उपयोग नहीं करना चुनते हैं, तो आपको प्रत्येक फायरबेस लाइब्रेरी संस्करण को उसकी निर्भरता पंक्ति में निर्दिष्ट करना होगा।
ध्यान दें कि यदि आप अपने ऐप में एकाधिक फायरबेस लाइब्रेरी का उपयोग करते हैं, तो हम लाइब्रेरी संस्करणों को प्रबंधित करने के लिए 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:20.4.0") }
Dart
अपने फ़्लटर ऐप में फ़ायरबेस जोड़ने के लिए निर्देशों का पालन करें।
अपने फ़्लटर प्रोजेक्ट के रूट से, प्लगइन इंस्टॉल करने के लिए निम्न कमांड चलाएँ:
flutter pub add cloud_functions
एक बार पूरा होने पर, अपने फ़्लटर एप्लिकेशन का पुनर्निर्माण करें:
flutter run
एक बार इंस्टॉल हो जाने पर, आप
cloud_functions
प्लगइन को अपने डार्ट कोड में आयात करके एक्सेस कर सकते हैं:import 'package:cloud_functions/cloud_functions.dart';
सी++
Android के साथ C++ के लिए:
- अपने C++ प्रोजेक्ट में Firebase जोड़ने के लिए निर्देशों का पालन करें।
- अपनी
CMakeLists.txt
फ़ाइल मेंfirebase_functions
लाइब्रेरी जोड़ें।
Apple प्लेटफ़ॉर्म के साथ C++ के लिए:
- अपने C++ प्रोजेक्ट में Firebase जोड़ने के लिए निर्देशों का पालन करें।
- क्लाउड फ़ंक्शंस पॉड को अपने
Podfile
में जोड़ें:pod 'Firebase/Functions'
- फ़ाइल सहेजें, फिर चलाएँ:
pod install
- अपने Xcode प्रोजेक्ट में Firebase C++ SDK से Firebase कोर और क्लाउड फ़ंक्शंस फ़्रेमवर्क जोड़ें।
-
firebase.framework
-
firebase_functions.framework
-
एकता
- अपने यूनिटी प्रोजेक्ट में फायरबेस जोड़ने के लिए निर्देशों का पालन करें।
- Firebase Unity SDK से
FirebaseFunctions.unitypackage
को अपने यूनिटी प्रोजेक्ट में जोड़ें।
क्लाइंट SDK प्रारंभ करें
क्लाउड फ़ंक्शंस का एक उदाहरण प्रारंभ करें:
तीव्र
lazy var functions = Functions.functions()
उद्देश्य सी
@property(strong, nonatomic) FIRFunctions *functions;
// ...
self.functions = [FIRFunctions functions];
वेब नेमस्पेस्ड एपीआई
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();
वेब मॉड्यूलर एपीआई
const app = initializeApp({
projectId: '### CLOUD FUNCTIONS PROJECT ID ###',
apiKey: '### FIREBASE API KEY ###',
authDomain: '### FIREBASE AUTH DOMAIN ###',
});
const functions = getFunctions(app);
Kotlin+KTX
private lateinit var functions: FirebaseFunctions // ... functions = Firebase.functions
Java
private FirebaseFunctions mFunctions; // ... mFunctions = FirebaseFunctions.getInstance();
Dart
final functions = FirebaseFunctions.instance;
सी++
firebase::functions::Functions* functions;
// ...
functions = firebase::functions::Functions::GetInstance(app);
एकता
functions = Firebase.Functions.DefaultInstance;
फ़ंक्शन को कॉल करें
तीव्र
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
}
}
उद्देश्य सी
[[_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"];
}];
वेब नेमस्पेस्ड एपीआई
var addMessage = firebase.functions().httpsCallable('addMessage');
addMessage({ text: messageText })
.then((result) => {
// Read result of the Cloud Function.
var sanitizedMessage = result.data.text;
});
वेब मॉड्यूलर एपीआई
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+KTX
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;
सी++
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);
}
एकता
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
कोड शामिल है। अपने कॉल करने योग्य फ़ंक्शन में त्रुटियों को संभालने के तरीके के लिए मार्गदर्शन देखें।
तीव्र
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 (error) {
if ([error.domain isEqual:@"com.firebase.functions"]) {
FIRFunctionsErrorCode code = error.code;
NSString *message = error.localizedDescription;
NSObject *details = error.userInfo[@"details"];
}
// ...
}
वेब नेमस्पेस्ड एपीआई
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;
// ...
});
वेब मॉड्यूलर एपीआई
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+KTX
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);
}
सी++
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);
// ...
एकता
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;
}
});
अनुशंसित: ऐप चेक के साथ दुरुपयोग रोकें
अपना ऐप लॉन्च करने से पहले, आपको यह सुनिश्चित करने में सहायता के लिए ऐप चेक सक्षम करना चाहिए कि केवल आपके ऐप ही आपके कॉल करने योग्य फ़ंक्शन एंडपॉइंट तक पहुंच सकते हैं।