问题描述
我最近发现了 Firebase可调用函数,它使我可以调用HTTPS触发器客户端的功能(并带有auth()支持).
I have recently discovered the Firebase callable functions which allows me to call HTTPS trigger like function from the client side (and with auth() support).
我很难在我现有的Firebase Web客户端应用程序中实现此新功能.
I struggle to implement this new feature in my already existing Firebase web-client application.
我正在运行一些云函数,其中有一些我想转换为HTTPS可调用函数的HTTPS函数(使用功能.https.onCall).
I have some cloud functions running, among them are some HTTPS functions I would like to transform into an HTTPS callable function (with functions.https.onCall).
文档表明:
Set up your client development environment
<script src="https://www.gstatic.com/firebasejs/4.12.0/firebase.js"></script>
<script src="https://www.gstatic.com/firebasejs/4.12.0/firebase-functions.js"></script>
我的代码是:
import firebase from 'firebase';
import 'firebase/firestore';
const firebaseApp = firebase.initializeApp({
apiKey: '....',
authDomain: '....',
databaseURL: '....',
projectId: '....',
storageBucket: '....',
messagingSenderId: '....',
});
const db = firebaseApp.firestore();
const auth = firebaseApp.auth();
const functions = firebaseApp.functions();
export { db, auth, functions };
运行我的应用程序时,出现以下错误:
When I run my app, I got the following error:
Uncaught TypeError: firebaseApp.functions is not a function
我尝试了yarn add firebase-functions
,然后尝试了import 'firebase-functions
,但随后该应用需要firebase-admin
.我很奇怪对于客户端应用程序来说太多了,所以我可能走错了方向.
I have tried yarn add firebase-functions
and then import 'firebase-functions
but then the app requires firebase-admin
. I am affraid it is too much for a client-app so I might go in the wrong direction.
有人可以帮助我解决这个问题吗?(!)关于云功能(节点JS)的服务器端Firebase SDK,此问题否.它是关于直接从Firebase Web应用程序调用Cloud函数.谢谢!
Can someone help me with this issue?(!) This issue is NOT about the server-side Firebase SDK for Cloud Functions (Node JS). It is about calling Cloud functions directly from a Firebase web app.Thank you!
更新:感谢@Andrew的帖子,这解决了我的问题:
UPDATE:Thanks to @Andrew's post, this solves my issue:
我的配置
import firebase from 'firebase';
import 'firebase/firestore';
import '@firebase/functions';
import firestoreConfig from '@/config/firestore';
const firebaseApp = firebase.initializeApp(firestoreConfig /* The JSON configuration from my Firebase project */);
const db = firebaseApp.firestore();
const auth = firebaseApp.auth();
const functions = firebaseApp.functions();
export { db, auth, functions };
使用配置:
import { db, functions } from '@/database/firestoreInit';
export default {
addMessage(text) {
const addMessage = functions.httpsCallable('addMessage');
return addMessage({ text }).then(result => result);
},
}
推荐答案
我自己遇到了同样的问题,并通过安装和导入 @ firebase/functions npm软件包解决了该问题.我在这里的github上找到了解决方案: https://github.com/firebase/firebase -js-sdk/blob/master/packages/functions/README.md
I just ran into this same problem myself and solved it by installing and importing the @firebase/functions npm package. I found the solution on github here:https://github.com/firebase/firebase-js-sdk/blob/master/packages/functions/README.md
从github上的README:
From the README on github:
ES模块
import firebase from '@firebase/app';
import '@firebase/functions'
// Do stuff w/ `firebase` and `firebase.functions`
CommonJS模块
const firebase = require('@firebase/app').default;
require('@firebase/functions');
// Do stuff with `firebase` and `firebase.functions`
希望有帮助!对于调用这些函数必须执行的操作,实际文档并不十分清楚.
Hope that helps! The actual documentation is not very clear about having to do this in order to call the functions.
这篇关于在Firebase客户端应用中实施可调用的云功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!