Vue + Firebase:函数 useEmulator() 被忽略

Vue + Firebase: Functions useEmulator() ignored

我在 Firebase docs firebase.functions().useEmulator('localhost', 5001) 中发现了这行代码,据说它会将您的 Vue 应用程序指向本地 运行ning 模拟器,但由于某种原因,我的项目忽略了该行代码并继续调用远程部署的函数。

我的 @/plugins/firebase.js 的相关部分如下所示:

import firebase from 'firebase/app';
import 'firebase/functions';

firebase.initializeApp({
  apiKey: process.env.VUE_APP_FIREBASE_API_KEY,
  authDomain: process.env.VUE_APP_FIREBASE_AUTH_DOMAIN,
  databaseURL: process.env.VUE_APP_FIREBASE_DATABASE_URL,
  projectId: process.env.VUE_APP_FIREBASE_PROJECT_ID,
  storageBucket: process.env.VUE_APP_FIREBASE_STORAGE_BUCKET,
  messagingSenderId: process.VUE_APP_FIREBASE_MESSAGE_SENDER_ID,
  appId: process.env.VUE_APP_FIREBASE_APP_ID,
  measurementId: process.env.VUE_APP_FIREBASE_MEASUREMENT_ID
});

firebase.functions().useEmulator('localhost', 5001);
const func = {
  botcheck: firebase.app().functions('europe-west2').httpsCallable('validateRecaptcha'),
};

export { func };

然后为了调用 botcheck 函数,我将 运行 在 Vuex 操作中执行以下操作:

const fb = require('@/plugins/firebase');
...
await fb.func.botcheck();

我做错了什么?如何让它正确指向我的本地 运行ning 模拟器?

Vue 项目版本:

函数项目版本:

如果我需要包含其他信息,请告诉我。

这是您的代码,因为我确保 useEmulator() 已使用 Cloud Functions for Firebase Emulator 正确配置。欢迎尝试:

import firebase from 'firebase/app';
import 'firebase/functions';

const firebaseConfig = {
 // Your config
};

const app = firebase.initializeApp(firebaseConfig);
const functions = app.functions('europe-west2');

functions.useEmulator('localhost', 5001);

const func = {
  botcheck: functions.httpsCallable('validateRecaptcha'),
};

export { func };

这一行:

firebase.functions()

functionally equivalent to:

firebase.app().functions('us-central1')

在您当前的代码中,您将未指定区域的函数连接到模拟器。因为你在使用的时候指定了区域为europe-west2,所以你需要在模拟器上连接europe-west2函数。您可以通过更改此行来执行此操作:

firebase.functions().useEmulator('localhost', 5001);

使用正确的区域:

firebase.app().functions('europe-west2').useEmulator('localhost', 5001)

补充说明:虽然firebase.functions()firebase.app().functions()return是同一个实例Functions object (connected to the us-central1 region), firebase.app().functions('us-central1') (where you pass in the region) returns a different instance of Functions。您需要将您使用的每个实例连接到模拟器。