Thursday, 8 June 2017

Algolia Nodejs Firebase syncing - .on('child_added') won't get called

I have a NodeJS script in which I'm watching trees in my Firebase database to see if an entry has been added/changed/removed via the child_added, childed_changed, and child_removed properties of the Firebase Admin .on() function in order to sync entries with my Algolia database.

The problem is that when I add/change/remove an item on these trees, the functions themselves never get fired.

However, when I start the script, I also have a .once('value', initialImport) function I call that works fine.

So for example, here is my relevant child_added code in my NodeJS script:

var dotenv = require('dotenv');
var firebaseAdmin = require("firebase-admin");
var algoliasearch = require('algoliasearch');

// load values from the .env file in this directory into process.env
dotenv.load();

var serviceAccount = require("./serviceAccountKey.json");
firebaseAdmin.initializeApp({
  credential: firebaseAdmin.credential.cert(serviceAccount),
  databaseURL: process.env.FIREBASE_DATABASE_URL
});
var database = firebaseAdmin.database();

var algolia = algoliasearch(process.env.ALGOLIA_APP_ID, process.env.ALGOLIA_API_KEY);
var users = algolia.initIndex('users');

var usersRef = database.ref("/social/users");

usersRef.on('child_added', addOrUpdateUserIndexRecord);

/**
 *  Add or update a band index record
 */
function addOrUpdateBandIndexRecord(dataSnapshot) {

  // Get Firebase object
  var firebaseObject = dataSnapshot.val();

  // Specify Algolia's objectID using the Firebase object key
  firebaseObject.objectID = dataSnapshot.key;

  // Add or update object
  bands.saveObject(firebaseObject, function(err, content) {
    if (err) {
      throw err;
    }
    console.log('Firebase<>Algolia band object saved', firebaseObject.objectID);
  });
}

Is there something I'm doing wrong? This used to work once upon a time.



via Stevie Star

No comments:

Post a Comment