Andrew B.
Andrew B.

Reputation: 275

Not all variables pass from Firestore to Algolia during onCreate() function

I am simply trying to get Firestore Functions to pass information from a newly made document from Firestore into Algolia index. Unfortunately I am only getting some, but NOT ALL, of the variables passed over and I don't know why. (Please review the images below for further context).

After Document has been made in Firestore enter image description here

What has been passed over to Algolia Index enter image description here

Please notice, not all the fields have been passed over. However, even inside the code (see below), I am trying to explicitly call those fields and have them pass. PLEASE HELP!

CODE

//////////////////////////////////////////////////////////////////////////////////
//Setting up and initializing.                                                  //
//////////////////////////////////////////////////////////////////////////////////

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

///////////////////////////////////////////////////////////////////////////////////
// Initialize Algolia, requires installing Algolia dependencies:                 //
// https://www.algolia.com/doc/api-client/javascript/getting-started/#install    //
// App ID and API Key are stored in functions config variables                   //
///////////////////////////////////////////////////////////////////////////////////

const ALGOLIA_ID = functions.config().algolia.appid;
const ALGOLIA_ADMIN_KEY = functions.config().algolia.apikey;
const ALGOLIA_SEARCH_KEY = functions.config().algolia.search_key;
const ALGOLIA_INDEX_NAME = 'searchable_courses';
const client = algoliasearch(ALGOLIA_ID, ALGOLIA_ADMIN_KEY);

////////////////////////////////////////////////////////////////////////////////////
//ADDING A FUNCTION THAT UPDATES THE INDEX EACH TIME A COURSE IS MADE             //
////////////////////////////////////////////////////////////////////////////////////

exports.onCourseCreated = functions.firestore.document('searchable_courses/{courseID}')
.onCreate((snap, context) => {

    // Get the newly made "course" document
    const course = snap.data();

    // Trying to add all the fields from the Firestore "course" document into Algolia index.
    course.objectID = context.params.courseID;
    course.courseID = context.params.courseID;
    course.courseTitle = context.params.courseTitle;
    course.authorUID = context.params.authorUID;
    course.description = context.params.description;
    course.subscriptionRate = context.params.subscriptionRate;
    course.numberOfSubscribers = context.params.numberOfSubscribers;
    course.datePosted = context.params.datePosted;
    course.numberOfLectures = context.params.numberOfLectures;
    course.numberOfQuestions = context.params.numberOfQuestions;

  
    // Write to the algolia index
    const index = client.initIndex(ALGOLIA_INDEX_NAME);
    return index.saveObject(course);
  });

Upvotes: 0

Views: 233

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317750

The data that arrives from the newly created document does not live in the context parameter. It lives in the snap parameter, which is a DocumentSnapshot object. Right now, you're overwriting all that data that you got out of the snapshot into the course variable.

You should just remove all the lines where you use context.parameter.X and pass the course object directly to Algolia as you got it from the document snapshot.

I suggest also reviewing the documentation for Firestore triggers to better understand how they work.

Upvotes: 1

Related Questions