Algolia脚本未与Firebase同步

时间:2019-02-24 23:32:37

标签: node.js firebase firebase-realtime-database algolia

我想拥有一个Algolia脚本,该脚本可以持续运行并检测Firebase的更改并基于该更新进行更新,但是当我在本地运行脚本时,它似乎不会触发任何更改,并且即使在我登录控制台时也无法打印功能。这是我脚本的一部分:

syncFirebaseToAlgolia.js

var dotenv = require('dotenv');

var Rebase = require('re-base');
var firebase = require('firebase');
var algoliasearch = require ('algoliasearch');

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

const config = {
  apiKey: process.env.REACT_APP_FIREBASE_KEY,
  authDomain: process.env.REACT_APP_FIREBASE_DOMAIN,
  databaseURL: process.env.REACT_APP_FIREBASE_DATABASE,
  projectId: process.env.REACT_APP_FIREBASE_PROJECT_ID,
  storageBucket: process.env.REACT_APP_FIREBASE_STORAGE_BUCKET,
  messagingSenderId: process.env.REACT_APP_FIREBASE_SENDER_ID
}

const firebaseDB = firebase.initializeApp(config)
const base = Rebase.createClass(firebase.database())

const algolia = algoliasearch(
    process.env.REACT_APP_ALGOLIA_APP_ID,
    process.env.REACT_APP_ALGOLIA_API_KEY,
    {protocol: 'https:'}
)
const wipsIndex = algolia.initIndex(process.env.REACT_APP_ALGOLIA_WIPS_INDEX_NAME)

const wipsRef = firebaseDB.database().ref('/WIPs');

wipsRef.on('child_added', addOrUpdateWIPIndexRecord);
wipsRef.on('child_changed', addOrUpdateWIPIndexRecord);
wipsRef.on('child_removed', deleteWIPIndexRecord);

function addOrUpdateWIPIndexRecord(wip) {
  console.log("ADD WIP")
  // Get Firebase object
  const record = wip.val();
  // Specify Algolia's objectID using the Firebase object key
  record.objectID = wip.key;
  // Add or update object
  wipsIndex
    .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 deleteWIPIndexRecord(wip) {
  // Get Algolia's objectID from the Firebase object key
  const objectID = wip.key;
  // Remove the object from Algolia
  wipsIndex
    .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);
    });
}

然后我运行node syncAlgoliaToFirebase,但似乎没有出现任何错误。

当我在base.js中包含这段代码并以npm start重新启动时,Algolia索引会正确更新。

我不知道我做错了什么,如果我不能在本地工作,我想知道如何在我的Digital Ocean液滴上工作。我会在小滴中运行node syncAlgoliaToFirebase吗?

0 个答案:

没有答案