簡體   English   中英

我正在嘗試從我的 flutter 移動應用程序中調用 firebase 雲功能。 我可以從 firebase 函數調用那些:shell 或本地

[英]I am trying to call firebase cloud functions from my flutter mobile application. I am able to call those from firebase functions : shell or locally

我正在嘗試從我的 flutter 移動應用程序中調用 firebase 雲功能,最初我在移動應用程序中運行 Firestore 邏輯中的所有邏輯,並開始知道在客戶端應用程序中運行這些東西是不安全的。 所以,現在我正在嘗試在 firebase 雲函數中實現邏輯,但是,我被困在這一點上超過 3 到 4 天,還請評論正確的方法來調用帶有參數的可調用 firebase ZC1C42502768E68384FC1AB45 我嘗試通過將區域傳遞給雲 function 並且沒有將區域傳遞給雲函數,但是這兩種方法都失敗了。 請幫我解決這個問題,或者在下面的評論中留下任何建議,任何幫助表示贊賞並提前感謝。

以下是我在函數和 flutter 項目中使用的代碼:

我的雲功能(使用 Oncall):

    import * as functions from "firebase-functions";
    import { usersRef } from "./config/firebase";
    
    const updateUserDataInDb = functions
      .region("asia-south1")
      .https.onCall((data, context) => {
        console.log(data);
        const userId = data.userId;
        const gender = data.gender;
        const phoneNumber = data.phoneNumber;
        const currentTime = Date.now();
        usersRef.doc(userId).update({
            gender: gender,
            phoneNumber: phoneNumber,
            deviceId: "FCM",
          });
          return {
            code: 200,
            message: "success",
          };
      });
    
    export { updateUserDataInDb };

我的 flutter 代碼:我嘗試按照人們在堆棧溢出和 GitHub 中的建議以多種方式更改代碼,但在我的情況下沒有任何效果。 請幫我解決這個問題,在此先感謝。

 method 1 :
 updateuserdata() async {
    print('invoking function');
    var params = {
      "userId": "1234",
      "gender": "male",
      "phoneNumber": "9999999999",
    };
    try {
      HttpsCallable callable =
          FirebaseFunctions.instanceFor(region: "asia-south1").httpsCallable(
              'updateUserDataInDb',
              options: HttpsCallableOptions(timeout: Duration(seconds: 5)));
      dynamic result = callable.call(params).catchError((onError) {
        print('function failed');
      });
      print(result);
    } catch (error) {
      print(error);
    }
  }
  method 2 : 
  updateuserdata() async {
    print('invoking function');
    var params = {
      "userId": "1234",
      "gender": "male",
      "phoneNumber": "9999999999",
    };
    try {
      HttpsCallable callable =
          FirebaseFunctions.instanceFor(region: "asia-south1").httpsCallable(
              'updateUserDataInDb',
              options: HttpsCallableOptions(timeout: Duration(seconds: 5)));
      dynamic result = callable(params);
      print(result);
    } catch (error) {
      print(error);
    }
  }
  method 3 : 
  updateuserdata() async {
    print('invoking function');
    var params = {
      "userId": "1234",
      "gender": "male",
      "phoneNumber": "9999999999",
    };
    try {
      HttpsCallable callable =
          FirebaseFunctions.instance.httpsCallable(
              'updateUserDataInDb',
              options: HttpsCallableOptions(timeout: Duration(seconds: 5)));
      dynamic result = callable.call(params);
      print(result);
    } catch (error) {
      print(error);
    }
  }
  method 4:
  updateuserdata() async {
    print('invoking function');
    var params = {
      "userId": "1234",
      "gender": "male",
      "phoneNumber": "9999999999",
    };
    try {
      HttpsCallable callable =
          FirebaseFunctions.instanceFor(app : Firebase.app(), region: "asia-south1").httpsCallable(
              'updateUserDataInDb',
              options: HttpsCallableOptions(timeout: Duration(seconds: 5)));
      dynamic result = callable.call(params).catchError((onError) {
        print('function failed');
      });
      print(result);
    } catch (error) {
      print(error);
    }
  }

我得到的錯誤:

Error 1 :
PlatformException (PlatformException(3840, The data couldn’t be read because it isn’t in the correct format., {message: The data couldn’t be read because it isn’t in the correct format., code: 3840}, null))

Error 2 :
Exception has occurred. PlatformException (PlatformException(not-found, NOT FOUND, {message: NOT FOUND, code: not-found}, null))

Error 3:
Error: Value for argument "documentPath" is not a valid resource path. Path must be a non-empty string.

Error 4:
throw PlatformException(code: errorCode, message: errorMessage as String?, details: errorDetails, stacktrace: errorStacktrace);

您正在定義params ,但從不將其發送到您的雲 function 因此雲 function 將在內部哀號,因為它找不到它期望的路徑。

看起來您並沒有await您的call()函數:

dynamic result = await callable.call(params)
    .catchError((onError) {
        print('function failed');
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM