squish
squish

Reputation: 1116

Why is my firebase function not reading my algolia keys from runtime config?

I keep getting the current error when trying to test my firebase function.

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)

My runtime config looks like this: enter image description here When I delete the config file and run firebase functions:config:get > .runtimeconfig.json it just gives me back these variables and the same file format, so I am a bit confused.

My function looks like this:

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(),
    });
  });

Upvotes: 0

Views: 1532

Answers (1)

samthecodingman
samthecodingman

Reputation: 26246

If you call this command using Powershell:

firebase functions:config:get > .runtimeconfig.json

The file .runtimeconfig.json is created using UCS-2 LE BOM encoding (which won't be read correctly by Node using the default settings).

Instead use this command that uses PowerShell piping:

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

Now .runtimeconfig.json will use the expected plain UTF-8 encoding.

Note: sc is the shorthand for the Set-Content cmdlet - which writes a file with the given contents (replacing the file if necessary). ac (the Add-Content cmdlet) is similar, but is used to append content to a file.

Upvotes: 13

Related Questions