Search code examples
node.jsfirebasegoogle-cloud-firestorealgolia

Initialising Algolia in Firebase Cloud functions


I am using the Algolia extension for Firebase. In Algolia I have an index with many docuemnts. Each user of my app should access only the documents they created. In order to implement this filter I need to generate a specific, filtered API key in Algolia for each user. I am trying to do this with a cloud function in Fireabse.

I get an error when I try to initialize Algolia in my local cloud functions index.js file and then deploy the functions.

Combining the docs of Cloud Functions and Algolia, I am doing this:

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

const db = admin.firestore();



// For the default version
const algoliasearch = require('algoliasearch');

// For the default version
// import algoliasearch from 'algoliasearch';

// For the search only version
// import algoliasearch from 'algoliasearch/lite';

const client = algoliasearch('appId', 'AdminApiKey');
const index = client.initIndex('profiles');

I haven't written any Algolia function yet, so I know that the error comes from this initialisation. (My other non-Aloglia cloud functions are running fine). And the Algolia search function installed automatically as a Firebase extension works fine too.

This is the error I get in the terminal when trying to deploying the cloud functions:

Function failed on loading user code. This is likely due to a bug in the user code. Error message: Error: please examine your function logs to see the error cause: https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs. Additional troubleshooting documentation can be found at https://cloud.google.com/functions/docs/troubleshooting#logging. Please visit https://cloud.google.com/functions/docs/troubleshooting for in-depth troubleshooting documentation.      

Functions deploy had errors with the following functions:
        writeToFirestore(us-central1)
i  functions: cleaning up build files...

Error: There was an error deploying functions

Thank you for any help!


Solution

  • So in the end the problem was that I had not installed the algoliasearch package in the Firebase Cloud Functions directory but in the parent (root) directory of the app.