簡體   English   中英

Firebase 從應用上下文調用的雲函數始終為 null

[英]Firebase Cloud Functions called from app context is always null

我按照 API 參考在一個 index.ts 文件中組織函數,( https://firebase.google.com/docs/functions/organize-functions ) 但是當檢查 firebase 控制台上下文中的日志時,控制台上下文始終是 null。

函數/src/index.ts

import * as functions from 'firebase-functions';
import { myFunction } from './myFireBaseFunctions'

const myFunction = functions.https.onCall(myFunctionHandler);

export default myFunction

函數/src/myFireBaseFunctions.ts

import * as functions from 'firebase-functions';

export const myFunction = async (context: functions.https.CallableContext) => {
  functions.logger.log('context', context);  // <-- null
  functions.logger.log('context.app', context.app);
  functions.logger.log('context.auth', context.auth);
  if (
    !context ||
    !context.app ||
    !context.auth ||
    !context.auth.token?.uid ||
    context.auth.token?.firebase?.sign_in_provider === 'anonymous' ||
    context.auth.token?.firebase?.email_verified === false
  ) {
    throw new functions.https.HttpsError(
      'unauthenticated',
      'must be called while authenticated.'
      );
    }
  }

  throw new functions.https.HttpsError(
    'unauthenticated',
    'must be called with different rights'
  );
};

// ...

當我從我的 web 應用程序調用 function 時:

@services/firebaseFunctions.service.ts

import { Functions, httpsCallable } from 'firebase/functions';

export const myFunction = ( functions: Functions ) => {
  const firebaseFunction = httpsCallable(functions, 'myFunction');
  return firebaseFunction();
};

// ...

我的組件.tsx

import React from 'react'
import { useFunctions } from 'reactfire';
import { myFunction } from '@services/firebaseFunctions.service';

const MyComponent = () => {
  const functions = useFunctions();

  useEffect(() => {
    const myFunctionCall = async () => {
      const res = await myFunction(functions);
      console.log('res.data', res.data);
    };
    myFunctionCall();
  }, []);

  return <div>MyComponent</>
}

export default MyComponent

失敗發生在 functions/src/myFireBaseFunctions.ts 中,需要指定數據。

export const myFunction = async (data:any, context: functions.https.CallableContext) => { // ... }

函數/src/myFireBaseFunctions.ts

import * as functions from 'firebase-functions';

export const myFunction = async (data:any, context: functions.https.CallableContext) => {
  functions.logger.log('context', context);  // <-- working now
  functions.logger.log('context.app', context.app);
  functions.logger.log('context.auth', context.auth);
  if (
    !context ||
    !context.app ||
    !context.auth ||
    !context.auth.token?.uid ||
    context.auth.token?.firebase?.sign_in_provider === 'anonymous' ||
    context.auth.token?.firebase?.email_verified === false
  ) {
    throw new functions.https.HttpsError(
      'unauthenticated',
      'must be called while authenticated.'
      );
    }
  }

  throw new functions.https.HttpsError(
    'unauthenticated',
    'must be called with different rights'
  );
};

// ...

暫無
暫無

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

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