[英]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.