我目前正在本地测试我的云功能。我找到了几种方法,但使用firebase模拟器和useFunctionsSimulator()方法似乎很棒。在https://firebase.google.com/docs/functions/local-emulator,他们没有说这个方法,但我在这个url上找到了它。如何在本地测试`functions.https.onCall`firebasecloudfunctions?。
但是,当我运行firebase模拟器时:start和console.log(firebase.functions().useFunctionsSimulator('http://localhost:5001'),它只是显示为未定义。
我尝试了几次关于原点的输入,但都没有改变。互联网上关于这件事的信息太少了,我想这是因为这是阿尔法,所以请帮我。
在初始化客户端中的firebase应用程序后,我通过调用useFunctionsSimulator()方法,让模拟器工作并处理本地请求。调用它之前会导致错误。
firebase.initializeApp(config);
firebase.functions().useFunctionsEmulator("http://localhost:5001");
useFunctionsEmulator()
不返回任何内容,它只是一个setter。
按以下方式使用:
firebase.initializeApp(config);
const functions = firebase.functions();
functions.useFunctionsEmulator("http://localhost:5001");
我也没能得到useFunctionsEmulator()
,但我有一个解决办法:
我将onCall
函数切换为onRequest
函数,如下所示:
// FROM THIS
exports.exampleFunction = functions.https.onCall((data, context) => {
// CODE FOR CLOUD FUNCTION
});
// TO THIS
exports.exampleFunction = functions.https.onRequest((request, response) => {
// CODE FOR CLOUD FUNCTION
});
然后,我可以用这个命令firebase serve --only functions
在本地服务我的功能,它将显示一个url
,我可以通过curl
、邮递员或我的浏览器向其发送请求。编辑完函数后,我将其切换回onCall
函数。我希望这能有所帮助!
对于Firebase 9(模块化),请改用connectFunctionsEmulator
:
import { getApp } from "firebase/app";
import { getFunctions, connectFunctionsEmulator } from "firebase/functions";
const functions = getFunctions(getApp());
connectFunctionsEmulator(functions, "localhost", 5001);
如果您在使用Firebase 9时遇到问题,请遵循本指南。
正如@app_所写,但这对某人来说可能也是值得的:
如果使用区域,则应仅为在线呼叫设置区域,而不是为模拟呼叫设置区域。这对我有效(JavaScript客户端):
const fns = LOCAL ? firebase.app().functions() :
firebase.app().functions(functionsRegion);
const log = fns.httpsCallable('logs_v200719');
如果我们知道我们是针对模拟后端运行的,那么LOCAL
会更早地设置为true
。请让我知道是否有一种标准的方法可以从Firebase客户端嗅探它。
根据开发人员的经验,最好在本地以相同的方式处理区域,这意味着不需要上面的三元运算符。
firebase工具8.6.0,JavaScript客户端7.16.1
调用时遇到问题
firebase.functions().useFunctionsSimulator("http://localhost:5001");
您可以通过添加此行在应用程序中测试onCall方法。
FirebaseFunctions functions = FirebaseFunctions.getInstance();
functions.useEmulator("10.0.2.2", 5001);
或
functions.UseFunctionsEmulator("http://localhost:5004");
以下是我的消息来源:https://firebase.google.com/docs/functions/local-emulator
EDIT:在node.js中也可以使用
const admin = require('firebase-admin');
admin.initializeApp();
admin.database().useEmulator('127.0.0.1', 5001);
唯一对我有用的是手动将firestore设置设置为localhost:
db.settings({
host: 'localhost:8080',
ssl: false
});
我的完整连接代码看起来像这个
const firebaseConfig = {
//your config Data from your firebase app
};
if (!firebase.apps.length) {
firebase.initializeApp(firebaseConfig);
}
var db = firebase.firestore();
db.settings({
host: 'localhost:8080',
ssl: false
});