Elias Fizesan
Elias Fizesan

Reputation: 305

Firebase function for Algolia returns undefined

I am following the Firebase tutorial on how to implement Algolia with Firebase: https://firebase.google.com/docs/firestore/solutions/search

I am currently stuck on the indexing part of the tutorial as I have errors coming from the firebase cloud-functions logs. This is the output of the cloud-functions log enter image description here

and this is the code I wrote

const functions = require('firebase-functions');

const algoliasearch = require("algoliasearch");

const ALGOLIA_ID = functions.config().algolia.app;
const ALGOLIA_ADMIN_KEY = functions.config().algolia.key;
const ALGOLIA_SEARCH_KEY = functions.config().algolia.search_key;

const ALGOLIA_INDEX_NAME = 'users';
const client = algoliasearch(ALGOLIA_ID, ALGOLIA_ADMIN_KEY);

// Update the search index every time a blog post is written.
exports.onUserCreated = functions.firestore.document('organisations/40R0LMA6ALZgF7KjHJMc/employees/{userId}').onCreate((snap, context) => {
    // Get the note document
    const user = snap.data();
  
    // Add an 'objectID' field which Algolia requires
    user.objectID = snap.id;
    console.log(user.objectID)
  
    // Write to the algolia index
    const index = client.initIndex(ALGOLIA_INDEX_NAME);
    return index.saveObject(user);
  });

Upvotes: 0

Views: 478

Answers (1)

Renaud Tarnec
Renaud Tarnec

Reputation: 83191

It seems that you are not correctly setting the different environment variables used in this example.

As explained in the doc, to get the value of the algolia.app environment variable when you do const ALGOLIA_ID = functions.config().algolia.app; you need to previously set its value as follows:

firebase functions:config:set algolia.app="THE_ALGOLIA_ID" 

Since you need to set several variables, you can set them in one instruction, as follows:

firebase functions:config:set algolia.app="THE_ALGOLIA_ID"  algolia.key="THE_ALGOLIA_ADMIN_KEY" ...

As explained in the doc, "to inspect what's currently stored in environment config for your project, you can use firebase functions:config:get" in the CLI.

Upvotes: 2

Related Questions