Q&A
React NativeアプリからFirebaseのCloud Functionにデプロイした関数をFirebase Javascript SDKを用いて実行したいと考えています。
試しに以下の関数をデプロイし、
ts
1exports.helloWorldTest = functions.https.onCall((data, context) => { 2 try { 3 console.log("hello test"); 4 console.log(data.text); 5 console.log(context); 6 return { 7 text: data.text, 8 }; 9 } catch (error) { 10 console.log(error); 11 return { 12 error: error, 13 }; 14 } 15});
ReactNativeアプリからボタン押下時に実行しようとしました。
tsx:App.tsx
1import React from 'react'; 2import { NativeBaseProvider, Button } from 'native-base'; 3import { FirebaseFunctions } from './src/firebase/config'; 4import { httpsCallable } from 'firebase/functions'; 5 6export default function App() { 7 const testPress = () => { 8 const helloWorld = httpsCallable(FirebaseFunctions, 'helloWorldTest'); 9 helloWorld({ text: 'test' }) 10 .then((res) => { 11 console.log(res.data); 12 }) 13 .catch((err) => { 14 console.log(err); 15 }); 16 }; 17 return ( 18 <NativeBaseProvider> 19 <Center> 20 <Button onPress={testPress}>テスト</Button> 21 </Center> 22 </NativeBaseProvider> 23 ); 24} 25
ts:src/firebase/config.ts
1import { initializeApp } from 'firebase/app'; 2import { getFunctions } from 'firebase/functions'; 3import { 4 FIREBASE_API_KEY, 5 FIREBASE_AUTH_DOMAIN, 6 FIREBASE_PROJECT_ID, 7 FIREBASE_STORAGE_BUCKET, 8 FIREBASE_MESSAGING_SENDER_ID, 9 FIREBASE_APP_ID, 10 FIREBASE_MEASUREMENT_ID, 11} from '@env'; 12 13const firebaseConfig = { 14 apiKey: FIREBASE_API_KEY, 15 authDomain: FIREBASE_AUTH_DOMAIN, 16 projectId: FIREBASE_PROJECT_ID, 17 storageBucket: FIREBASE_STORAGE_BUCKET, 18 messagingSenderId: FIREBASE_MESSAGING_SENDER_ID, 19 appId: FIREBASE_APP_ID, 20 measurementId: FIREBASE_MEASUREMENT_ID, 21}; 22 23export const FirebaseApp = initializeApp(firebaseConfig); 24export const FirebaseFunctions = getFunctions(FirebaseApp); 25
その結果、以下のエラーが出力されました。
internal at node_modules/@firebase/component/dist/esm/index.esm2017.js:326:27 in Provider#shouldAutoInitialize at node_modules/@firebase/util/dist/index.esm2017.js:508:0 in <global> at node_modules/tslib/tslib.js:64:4 in <anonymous> at http://100.64.1.41:19000/node_modules/expo/AppEntry.bundle?platform=ios&dev=true&hot=false&strict=false&minify=false:203807:25 in FunctionsError at node_modules/tslib/tslib.js:198:21 in __spreadArrays at node_modules/@firebase/functions/dist/index.esm2017.js:154:21 in <global>
また、firebaseコンソールからCloud Functionのログを見ても関数は呼び出されていないようです。
shouldAutoInitializeと表示されているのでinitializeがうまくいっていないのかなと考えているのですが、原因がよくわかりません。
ご存知の方いらっしゃいましたらご教授いただけると幸いです。
よろしくお願いします。
回答1件
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。