与Algolia进行实时同步的问题

时间:2019-04-23 01:20:57

标签: firebase firebase-realtime-database synchronization algolia

我正在尝试运行一个脚本,该脚本将与Firebase的阿尔戈利亚保持实时同步。这是要运行的脚本:

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);
      });
  }

关闭用于进行实时同步的脚本后,我填充了我的Firebase,然后运行了该脚本,效果很好,但是如果脚本打开并且我填充了我的Firebase,则会出现此错误:

  

将联系人编入Algolia {AlgoliaSearchError:JSON   root必须是在第1行:2列附近的对象(不允许使用字符串)

0 个答案:

没有答案