为什么我的 firebase 函数不从运行时配置中读取我的 algolia 密钥?
Why is my firebase function not reading my algolia keys from runtime config?
我在尝试测试我的 firebase 函数时不断收到当前错误。
Found .runtimeconfig.json but the JSON format is invalid.
! TypeError: Cannot read property 'app' of undefined
at Object.<anonymous> (F:\Web Dev Stuff\SEEKIO\seekio\functions\index.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:\Users\msi\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.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
编码。
注:与sc
是shorthand为Set-Content
cmdlet - which writes a file with the given contents (replacing the file if necessary). ac
(the Add-Content
cmdlet)类似,只是用来追加内容到一个文件中。
我在尝试测试我的 firebase 函数时不断收到当前错误。
Found .runtimeconfig.json but the JSON format is invalid.
! TypeError: Cannot read property 'app' of undefined
at Object.<anonymous> (F:\Web Dev Stuff\SEEKIO\seekio\functions\index.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:\Users\msi\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.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
编码。
注:与sc
是shorthand为Set-Content
cmdlet - which writes a file with the given contents (replacing the file if necessary). ac
(the Add-Content
cmdlet)类似,只是用来追加内容到一个文件中。