为什么我的firebase函数没有从运行时配置中读取我的algolia密钥?

尝试测试我的 firebase 功能时,我不断收到当前错误。

Found .runtimeconfig.json but the JSON format is invalid.
!  TypeError: Cannot read property 'app' of undefined
    at Object.<anonymous> (F:Web Dev StuffSEEKIOseekiofunctionsindex.js:4:56)
    at Module._compile (internal/modules/cjs/loader.js:701:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
    at Module.load (internal/modules/cjs/loader.js:600:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
    at Function.Module._load (internal/modules/cjs/loader.js:531:3)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:22:18)
    at initializeRuntime (C:UsersmsiAppDataRoamingnpmnode_modulesfirebase-toolslibemulatorfunctionsEmulatorRuntime.js:680:29)
    at process._tickCallback (internal/process/next_tick.js:68:7)
!  We were unable to load your functions code. (see above)

我的运行时配置如下所示:

当我删除配置文件并运行firebase functions:config:get > .runtimeconfig.json它时,它只会返回这些变量和相同的文件格式,所以我有点困惑。

我的函数如下所示:

const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();

const algoliasearch = require("algoliasearch");
const client = algoliasearch(
  functions.config().algolia.app,
  functions.config().algolia.key
);
const index = client.initIndex("listings");

exports.createRecord = functions
  .region("europe-west1")
  .database.ref("/listings/{listingID}")
  .onCreate((snap, context) => {
    return index.saveObject({
      objectID: snap.id,
      ...snap.data(),
    });
  });

回答

如果您使用 Powershell 调用此命令:

firebase functions:config:get > .runtimeconfig.json

该文件.runtimeconfig.json是使用UCS-2 LE BOM编码创建的(Node 使用默认设置无法正确读取)。

而是使用这个使用 PowerShell 管道的命令:

firebase functions:config:get | sc .runtimeconfig.json

现在.runtimeconfig.json将使用预期的普通UTF-8编码。

注意: scSet-Contentcmdlet的简写- 用给定的内容写入文件(如有必要,替换文件)。acAdd-Contentcmdlet)类似,但用于将内容附加到文件。


以上是为什么我的firebase函数没有从运行时配置中读取我的algolia密钥?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>