简体   繁体   English

Firebase 从应用上下文调用的云函数始终为 null

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

I followed the API reference for organizing functions in one index.ts file, ( https://firebase.google.com/docs/functions/organize-functions ) but when checking the logs in the firebase console context is always null.我按照 API 参考在一个 index.ts 文件中组织函数,( https://firebase.google.com/docs/functions/organize-functions ) 但是当检查 firebase 控制台上下文中的日志时,控制台上下文始终是 null。

functions/src/index.ts函数/src/index.ts

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

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

export default myFunction

functions/src/myFireBaseFunctions.ts函数/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'
  );
};

// ...

when I call the function from my web app:当我从我的 web 应用程序调用 function 时:

@services/firebaseFunctions.service.ts @services/firebaseFunctions.service.ts

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

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

// ...

myComponent.tsx我的组件.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

The failure was in functions/src/myFireBaseFunctions.ts data needs to be specified.失败发生在 functions/src/myFireBaseFunctions.ts 中,需要指定数据。

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

functions/src/myFireBaseFunctions.ts函数/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.

相关问题 Express 应用程序是否始终在 Firebase Cloud Functions 中运行? - Is an express app always running in Firebase Cloud Functions? Firebase Cloud Functions - 从 https 中的上下文获取来源可调用 function - Firebase Cloud Functions - get origin from context in https callable function 与 firebase 云函数中的 firebase firestore 交互 - Interacting with firebase firestore from firebase cloud functions 避免客户端工作的 Firebase Cloud 功能总是值得的吗? - Firebase Cloud functions to avoid client side work is always worth? Google Cloud Functions Firebase 错误 默认的 Firebase 应用已经存在 - Google Cloud Functions Firebase Error The default Firebase app already exists 未收到通过 iOS 应用程序中的 firebase 云功能发送的推送通知,尽管我从消息控制台获取 - Not receiving push notification sent via firebase cloud functions in iOS app though I get the from messaging console Firebase RemoteConfig 是否可以从云功能访问 - Can Firebase RemoteConfig be accessed from cloud functions 从 firebase 函数返回数据返回 null - Returning data from firebase functions returns null 关于在 firebase 云函数中 promise 的 then 回调中返回 null 值的问题 - Question regarding returning a null value in a then callback of a promise in firebase cloud functions Cloud Functions for Firebase 超时 - Cloud Functions for Firebase timeout
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM