Search code examples
node.jsfirebasegoogle-cloud-functionsalgoliafirebase-cli

Error Deploying Firebase function


I am trying to deploy a firebase cloud function that will push something to my algolia search engine everytime a new event is added under the events node. I followed the tutorial here which seems to detail how to do things for the most part.

https://www.youtube.com/watch?v=Njtbo3YUdH4

However, when I run my project i get the strangest error Error occurred while parsing your function triggers.

TypeError: Cannot read property 'config' of undefined

I have never seen this error when trying to deploy functions in the past. Im not sure if something changed or not but I can't really find where to fix this error on firebase docs or stackoverflow so far.

I am running node version v8.11.3 in addition to that I am pretty sure that I have my algolia configured properly due to the fact that when I run

$ firebase functions:config:get

I get this

{
  "algolia": {
    "appid": "xxxxxxx",
    "adminkey": "xxxxxxxxxx"
  }
}

The only thing it could possibly be is my code. I will include it below if anyone could help me fix it I would greatly appreciate it.

//similar to import functions in swift
const functions = require('firebase-functions');
// The Firebase Admin SDK to access the Firebase Realtime Database.
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
//exports acts as a global variable that you can init other properties on
//on
// exports.helloWorld = functions.https.onRequest((request, response) => {
//  response.send("Hello from Firebase!");
// });
const algoliasearch = require('algoliasearch');
const algolia = algoliasearch(functions.config().algolia.appid,functions.algolia.config().adminKey)

exports.updateIndex = functions.database.ref('/events/{eventKey}').onWrite(event =>{
    const index = algolia.initIndex('events');
    const eventKey = event.params.eventKey
    const data = event.data.val();

    if(!data){
        return index.deleteObject(eventKey,(err) =>{
            if (err) throw err
            console.log('Event deleted from algolia index',eventKey)
        })
    }

    data['objectID'] = eventKey
    return index.saveObject(data,(err,content) =>{
        if (err) throw err
        console.log('Event updated in algolia index',data.objectID)
    })
});

Solution

  • Look at this line (I added a carriage return):

    const algolia = algoliasearch(functions.config().algolia.appid,
        functions.algolia.config().adminKey)
    

    This looks correct:

    functions.config().algolia.appid
    

    This doesn't:

    functions.algolia.config().adminKey
    

    It looks like you meant to say this instead:

    functions.config().algolia.adminKey