NodeJS Firebase函数在尝试从Unity调用时抛出内部错误

ddarikpa  于 2023-05-28  发布在  Node.js
关注(0)|答案(1)|浏览(205)

我在一个Unity C#应用程序上工作,安装了Firebase Auth和Firebase Functions SDK。
感谢Firebase模拟器,我开发并测试了三个可调用函数,它们似乎可以正常工作。
我部署了我的函数,在Unity编辑器中启动了游戏,每当我调用函数时都会出现错误。错误始终为INTERNAL
我检查了Google Cloud Jmeter 板上的日志,似乎我的功能甚至没有运行。
我设置了一个断点,并决定检查异常以收集一些信息。但是,除了"One or more errors occurred""INTERNAL"之外,没有任何有用的信息。
在Google上搜索了很多次之后,我发现了两个可能的罪魁祸首:

*过时的SDK->我重新下载了最新的SDK,并将Firebase Auth和Firebase Functions重新导入到我的Unity项目中-->相同的错误。
*获取Functions示例时,如果不是us-central1,需要指定region。根据Google Jmeter 板,我的函数位于us-central1区域,但以防万一我无论如何都指定它-->相同的错误。然后我注意到我的实际Firebase项目是在europe-west3中,所以我在代码中指定了这个,当获取Firebase Functions或App的示例时-->仍然是相同的错误。

现在我开始怀疑一切,所以我注解掉UseFunctionsEmulator()并再次使用模拟器进行测试->它工作正常。
会有什么问题呢?
下面是我的FirebaseManager.cs,它在其他任何东西之前运行:

public class FirebaseManager : MonoBehaviour
{
    private static bool gameQuitting = false;
    private static readonly object Lock = new object();
    private static FirebaseManager _instance;
    public static FirebaseManager Instance
    {
        get
        {
            // [check if instance exists etc..]
        }
    }

    private FirebaseApp _app;

    private FirebaseAuth _auth;
    public FirebaseAuth Auth
    {
        get
        {
            if (_auth == null)
            {
                _auth = FirebaseAuth.GetAuth(_app);
            }
            return _auth;
        }
    }

    private FirebaseUser _user;
    public FirebaseUser User
    {
        get
        {
            return _user;
        }
        set
        {
            _user = value;
        }
    }

    private FirebaseFunctions _func;
    public FirebaseFunctions Func
    {
        get
        {
            if (_func == null)
            {
                _func = FirebaseFunctions.DefaultInstance;
            }
            return _func;
        }
    }

    // Events to subscribe to for this service
    public delegate void FirebaseInitialized();
    public static event FirebaseInitialized OnFirebaseInitialized;
    public static bool IsInitialized = false;

    private void Awake()
    {
        if (_instance == null)
        {
            DontDestroyOnLoad(this.gameObject);
            _instance = this;
        }
        else { Destroy(this.gameObject); }
    }

    private void Start()
    {
        InitializeFirebase();
    }

    private void InitializeFirebase()
    {
        Debug.Log($"FirebaseManager.cs > INITIALIZING FIREBASE");
        Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWithOnMainThread(task =>
        {
            var dependencyStatus = task.Result;
            if (dependencyStatus == Firebase.DependencyStatus.Available)
            {
                // Create and hold a reference to your FirebaseApp,
                // where app is a Firebase.FirebaseApp property of your application class.
                _app = FirebaseApp.DefaultInstance;
                // _app = FirebaseApp.GetInstance("europe-west3");

                // Create and hold a reference to FirebaseAuth
                _auth = FirebaseAuth.DefaultInstance;

                // Create and hold a reference to already logged in user (if any)
                _user = _auth.CurrentUser;

                // Create and hold a reference to Functions
                _func = FirebaseFunctions.DefaultInstance;
                // _func = FirebaseFunctions.GetInstance(FirebaseApp.DefaultInstance, "europe-west3");
                // _func = FirebaseFunctions.GetInstance("europe-west3");
                // #if UNITY_EDITOR
                // _func.UseFunctionsEmulator("http://localhost:5001"); // LOCAL FUNCS 
                // #endif

                // Subscribing to AuthStateChanged
                FirebaseManager.Instance.Auth.StateChanged += AuthService.AuthStateChanged;

                Debug.Log($"FirebaseManager.cs > INITIALIZED");
                IsInitialized = true;
                if (OnFirebaseInitialized != null) OnFirebaseInitialized.Invoke();
            }
            else
            {
                UnityEngine.Debug.LogError(System.String.Format(
                  "FirebaseManager.cs > Could not resolve all Firebase dependencies: {0}", dependencyStatus));
                // Firebase Unity SDK is not safe to use here.
            }
        });
    }

    private void OnDestroy()
    {
        if (_auth != null) _auth = null;
    }

    private void OnApplicationQuit()
    {
        FirebaseManager.Instance.Auth.StateChanged -= AuthService.AuthStateChanged;
        gameQuitting = true;
    }
}

我从Unity调用我的函数如下:

HttpsCallableReference function = FirebaseManager.Instance.Func.GetHttpsCallable("GetAllCharacters");

// Call the function and extract the operation from the result.
function.CallAsync().ContinueWithOnMainThread((task) =>
{
    if (task.IsFaulted)
    {
        // this foreach is a debug attempt
        foreach (var e in task.Exception.Flatten().InnerExceptions)
        {
            Debug.LogWarning($"Received Exception: {e.Message}");
        }

        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.Message;

               Debug.LogError($"Code: {code} // Message: {message}");

               // [Handle error]
            }
        }
    }
    else
    {
        // [Handle data returned by the function]
    }
});

最后,我的Firebase函数看起来像这样:(index.ts

import * as Characters from "./Characters/Characters";

// CHARACTERS
export const CreateNewCharacter = Characters.CreateNewCharacter;
export const GetAllCharacters = Characters.GetAllCharacters;
export const DeleteCharacterByID = Characters.DeleteCharacterByID;

Characters.ts

import * as functions from "firebase-functions";
import { initializeApp } from "firebase-admin/app";
// [custom classes imports]

// DATABASE INITIALIZATION
initializeApp();
const db = getFirestore();
 
// CREATE NEW CHARACTER
export const CreateNewCharacter =
  functions.https.onCall(async (data, context) =>
  {
      ...
  });

// GET ALL CHARACTERS
export const GetAllCharacters =
  functions.https.onCall(async (data, context) =>
  {
      ...
  });

// DELETE CHARACTER BY ID
export const DeleteCharacterByID =
  functions.https.onCall(async (data, context) =>
  {
      ...
  });

在逐步调试中,我发现:

StackTrace的值为:
“在Firebase.Functions.HttpCallableReference.b__9_0(System.Threading.Tasks.Task'1[TResult] task)[0x00063] in<9b40ff23f61a4fcf97a5710997860279>:0 \n在System.Threading.Tasks.ContinuationResultTaskFromResultTask`2[TAntecedentResult,TResult].InnerInvoke()[0x00024] in<4ba27aa039714eafa1e8892e51af2932>:0 \n在System.Threading.Tasks.Task.Execute()[0x00000] in<4ba27aa039714eafa1e8892e51af2932>:0“

cwtwac6a

cwtwac6a1#

我今天也犯了同样的错误。删除firebase控制台中的函数并重新部署它为我解决了这个问题,同时仍然使用默认示例。

相关问题