1

我有一个nuxt应用程序上传到 Firebase(作为云功能)。我还有一个我试图从应用程序调用的可调用函数。问题是它试图调用localhosturl 而不是生产的。

我的 Firebase 设置nuxt.config.js如下所示:

module.exports = {
    env: {
        functionsURL: process.env.NUXT_ENV_FUNCTIONS === 'local' ? "http://localhost:5001/turniejomat/us-central1" : 'https://us-central1-turniejomat.cloudfunctions.net', // I would expect this to be applied
    },
    modules: [
        [
            '@nuxtjs/firebase',
            {
                config: {
                    // app config
                },
                services: {
                    functions: {
                        location: 'us-central1',
                        emulatorPort: 5001,
                    }
                }
            }
        ]
    ],
}

firebase.nuxt.org文档仅提及模拟器配置,但未提及生产。

我这样调用函数:

const signUp = await this.$fire.functions.httpsCallable("signup")(configObject)

如何使功能在生产中使用正确的 url?

编辑:

这是package.json设置:

"scripts": {
        "dev": "SET \"NUXT_ENV_FUNCTIONS=local\" & nuxt",
        "build": "SET \"NUXT_ENV_FUNCTIONS=fire\" & nuxt build",
 } 

编辑2:

显然env.functionsURL应用正确,因为应用程序代码直接将此变量用于其他目的并且它可以正常工作!这是callable functions唯一一个由于某种原因没有收到要调用的相关生产 url。同时,代码中唯一出现 5001 端口的地方是:

  1. nuxt.config.js / env环境
  2. nuxt.config.js / modules / services / functions / emulatorPort环境
  3. service.functions.js自动添加的文件夹中的模块nuxt/firebase(我猜是通过 firebase.nuxtjs 吗?)。

该模块如下所示:

export default async function (session) {
  await import('firebase/functions')
  const functionsService = session.functions('us-central1')
  functionsService.useFunctionsEmulator('http://localhost:5001')
  return functionsService
}

所以也许出于某种原因callable function认为它仍然应该使用模拟器设置?我怎么能阻止它?

调用模块的唯一位置是nuxt/firebase/index.js,如下所示:

 if (process.server) {
    servicePromises = [
      authService(session, firebase, ctx, inject),
    firestoreService(session, firebase, ctx, inject),
    functionsService(session, firebase, ctx, inject),
    ]
  }

  if (process.client) {
    servicePromises = [
      authService(session, firebase, ctx, inject),
      firestoreService(session, firebase, ctx, inject),
      functionsService(session, firebase, ctx, inject),
    ]
  }

这似乎确实不管环境如何,Firebase 的本机代码确实应用了相同的设置。我可以修改functionsService代码,但它似乎不是最佳解决方案,因为 Firebase 可能会在某些时候覆盖它,例如在构建或更新期间。或者可能是这些“本机”文件仅在开始时生成并且尽管在配置中进行了潜在更改(这是不正确的,但现在是正确的),但并未更新。

我怎样才能强制更改这些 Firebase 的文件以区分 prod 和 dev 环境并使它们安全地持续存在?可能nuxt.config.js / modules / services / functions /应该配置不同,但是如何?

4

3 回答 3

1

所以解决方案是有条件地设置functions.emulatorPort设置nuxt.config.js- 仅适用于开发环境和undefined生产环境:

functions: {
    location: 'us-central1',
    emulatorPort: process.env.NUXT_ENV_FUNCTIONS === 'local' ? 5001 : undefined,                    
}
于 2021-10-29T14:15:19.473 回答
0

使用publicRuntimeConfigin nuxt.config.js,为了配置环境:

export default {
  publicRuntimeConfig: {
    baseURL: process.env.BASE_URL || 'https://us-central1-turniejomat.cloudfunctions.net'
  }
}

随后dev脚本将是:

"dev": "SET \"BASE_URL=http://localhost:5001/turniejomat/us-central1\" & nuxt"

始终用开发价值代替生产,而不是相反。

于 2021-10-13T14:20:11.443 回答
0

您可能需要再次查看文档。我检查了前面的步骤,您似乎没有初始化 firebase,您还可以指定是要在开发模式下工作还是在生产模式下工作。你可以在指南中找到这个,配置在开头。( guide/options/config )nuxt.config.js文件的详细信息。

配置

必需的

您的 Firebase 配置片段和其他 Firebase 特定参数。您可以从 Firebase 项目的概览页面检索此信息:

https://console.firebase.google.com/project/<your-project-id>/overview

nuxt.config.js

config: {
  // REQUIRED: Official config for firebase.initializeApp(config):
  apiKey: '<apiKey>',
  authDomain: '<authDomain>',
  projectId: '<projectId>',
  storageBucket: '<storageBucket>',
  messagingSenderId: '<messagingSenderId>',
  appId: '<appId>',
  measurementId: '<measurementId>'
}

如果放在子对象 config.production 和 config.development 中,可以按NODE_ENV环境定义,这意味着例如 config.production 在 NODE_ENV === 'production' 时被加载。

您还可以指定多个自定义环境,如下面的 customEnv选项中所述。

指南/选项/服务部分会 指导您如何初始化 firebase。

服务

必需的

默认情况下,不会初始化任何 Firebase 产品要初始化特定服务,请将其 services 标志设置为 true 或创建一个子对象并在该服务之后命名键。

可用服务:

nuxt.config.js

services: {
  auth: true,
  firestore: true,
  functions: true,
  storage: true,
  database: true,
  messaging: true,
  performance: true,
  analytics: true,
  remoteConfig: true
} 

每个服务都有您可以配置的高级选项。有关详细信息,请参阅服务选项部分。

编辑 如果这不起作用,我想知道您是否在开发模式下配置了您的 firebase 项目,在这种情况下,问题可能存在。要将其更改为开发模式或对该部分进行故障排除,您可能需要从头开始创建一个新项目并像他们在此问题中建议的那样导入/导出旧项目。

于 2021-10-14T23:16:04.470 回答