On-going live sync with Algolia from firebase!

I’m trying to run a script that will keep an live sync with algolia from firebase. This is the script to run:

const algoliasearch = require('algoliasearch');
const dotenv = require('dotenv');
const firebase = require('firebase');

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

// configure firebase
firebase.initializeApp({
  databaseURL: process.env.FIREBASE_DATABASE_URL,
});
const database = firebase.database();

// configure algolia
const algolia = algoliasearch(
  process.env.ALGOLIA_APP_ID,
  process.env.ALGOLIA_API_KEY
);
const index = algolia.initIndex(process.env.ALGOLIA_INDEX_NAME);


  // Add or update new objects
  const contactsRef = database.ref('/ctpwebsite-c33ff/guitars');
  contactsRef.on('child_added', addOrUpdateIndexRecord);
  contactsRef.on('child_changed', addOrUpdateIndexRecord);
  contactsRef.on('child_removed', deleteIndexRecord);

  function addOrUpdateIndexRecord(contact) {
    // Get Firebase object
    const record = contact.val();
    // Specify Algolia's objectID using the Firebase object key
    record.objectID = contact.key;
    // Add or update object
    index
      .saveObject(record)
      .then(() => {
        console.log('Firebase object indexed in Algolia', record.objectID);
      })
      .catch(error => {
        console.error('Error when indexing contact into Algolia', error);
        process.exit(1);
      });
  }

  function deleteIndexRecord({key}) {
    // Get Algolia's objectID from the Firebase object key
    const objectID = key;
    // Remove the object from Algolia
    index
      .deleteObject(objectID)
      .then(() => {
        console.log('Firebase object deleted from Algolia', objectID);
      })
      .catch(error => {
        console.error('Error when deleting contact from Algolia', error);
        process.exit(1);
      });
  }

Once my script for on-going live sync is off and I populate my firebase and then run the script it works fine, but if the script is on and i populate my firebase I get this error:

Error when indexing contact into Algolia { AlgoliaSearchError: JSON
root must be an object (string not allowed) near line:1 column:2

What am i doing wrong?

When calling .saveObject(record) there must be some records that are not well formatted.
Could you log into Firebase (https://firebase.google.com/docs/functions/writing-and-viewing-logs) the objects that are in error? And then send us the update. Thanks!

To do so, just change the lines to:

.catch(error => {
  console.log(record);
  console.error('Error when indexing contact into Algolia', error);
  process.exit(1);
});

Error when indexing contact into Algolia { AlgoliaSearchError: JSON root must be an object (string not allowed) near line:1 column:2
at success (/Users/myname/ctp-website/NodeJS/node_modules/algoliasearch/src/AlgoliaSearchCore.js:375:32)
at process._tickCallback (internal/process/next_tick.js:68:7)
name: ‘AlgoliaSearchError’,
message:
‘JSON root must be an object (string not allowed) near line:1 column:2’,
debugData:
[ { currentHost: ‘https://n2chmivtjk.algolia.net’,
headers: [Object],
content: ‘""’,
contentLength: 2,
method: ‘PUT’,
timeouts: [Object],
url: ‘/1/indexes/guitar/undefined’,
startTime: 2019-04-23T10:39:21.926Z,
endTime: 2019-04-23T10:39:21.960Z,
duration: 34,
statusCode: 400 } ],
statusCode: 400 }

this is the error that i am getting

still not fixed, any ideas on what could be the issue. Or any work around this as i want to host this file on a server, could i run it only when my firebase database has been updated and not listen for it?

Forgive me if this isn’t useful, but given " JSON root must be an object" I’d assume the JSON you got from firebase is a string and you simply need to JSON.stringify…

  function addOrUpdateIndexRecord(contact) {
    // Get Firebase object
    const record = JSON.stringify(contact.val());
...