Algolia / Firebase - 终端执行代码然后闲置

时间:2017-02-14 03:50:52

标签: node.js firebase terminal command-line-interface algolia

我遵循了这些Algolia/Firebase docs。我做了一些调整,因为一些初始设置说明已经过时,但我得到了它的工作。我将该页面中的所有代码放在app.js文件中。在终端内部,我运行'npm start',当我检查我的Algolia控制台时,Indices成功索引了我的Firebase数据。

app.js

var admin = require("firebase-admin");
admin.initializeApp({
  credential: admin.credential.cert({
    projectId: "*****",
    clientEmail: "*****",
    privateKey: "-----BEGIN PRIVATE KEY-----\n...code...\n-----END PRIVATE KEY-----\n"
  }),
  databaseURL: "*****.firebaseio.com"
});

var firebase = require('firebase');

var config = {
    apiKey: "*****",
    authDomain: "*****.firebaseapp.com",
    databaseURL: "*****.firebaseio.com",
    storageBucket: "*****.appspot.com",
    messagingSenderId: "*****"
  };
  firebase.initializeApp(config);

var algoliasearch = require('algoliasearch');
var client = algoliasearch('*******', '***...**');
var index = client.initIndex('posts');

// Connect to our Firebase contacts data
var rootRef = firebase.database().ref('posts');

// Get all data from Firebase
rootRef.on('value', initIndex);

function initIndex(dataSnapshot) {
  // Array of data to index
  var objectsToIndex = [];
  // Get all objects
  var values = dataSnapshot.val();
  // Process each Firebase ojbect

  for (var key in values) {
    if (values.hasOwnProperty(key)) {
      // Get current Firebase object

      var firebaseObject = values[key];
      // Specify Algolia's objectID using the Firebase object key
      firebaseObject.objectID = key;

      // Add object for indexing
      objectsToIndex.push(firebaseObject);
    }
  }
  // Add or update new objects
  index.saveObjects(objectsToIndex, function(err, content) {
    if (err) {
      throw err;
    }
    console.log('Firebase<>Algolia import done');
  });
}

rootRef.on('value', reindexIndex);
function reindexIndex(dataSnapshot) {
  // Array of objects to index
  var objectsToIndex = [];
  // Create a temp index
  var tempIndexName = 'contacts_temp';
  var tempIndex = client.initIndex(tempIndexName);
  // Get all objects
  var values = dataSnapshot.val();
  // Process each Firebase object
  for (var key in values) {
    if (values.hasOwnProperty(key)) {
      // Get current Firebase object
      var firebaseObject = values[key];
      // Specify Algolia's objectID using the Firebase object key
      firebaseObject.objectID = key;
      // Add object for indexing
      objectsToIndex.push(firebaseObject);
    }
  }
  // Add or update new objects
  index.saveObjects(objectsToIndex, function(err, content) {
    if (err) {
      throw err;
    }
    // Overwrite main index with temp index
    client.moveIndex(tempIndexName, 'contacts', function(err, content) {
      if (err) {
        throw err;
      }
      console.log('Firebase<>Algolia reimport done');
    });
  });
}

// Listen for changes to Firebase data
rootRef.on('child_added', addOrUpdateObject);
rootRef.on('child_changed', addOrUpdateObject);
function addOrUpdateObject(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
  index.saveObject(firebaseObject, function(err, content) {
    if (err) {
      throw err;
    }
    console.log('Firebase<>Algolia object saved');
  });
}

// Listen for changes to Firebase data
rootRef.on('child_removed', removeIndex);
function removeIndex(dataSnapshot) {
  // Get Algolia's objectID from the Firebase object key
  var objectID = dataSnapshot.key;
  // Remove the object from Algolia
  index.deleteObject(objectID, function(err, content) {
    if (err) {
      throw err;
    }
    console.log('Firebase<>Algolia object deleted');
  });
}

终端执行以下声明:

>node app.js

Firebase<>Algolia reimport done
Firebase<>Algolia object saved
Firebase<>Algolia import done
//terminal just sits idle from this point on

正如我所说,该对象已编入索引(我在Firebase中只有一个对象)。

问题出在最后的Firebase<>Algolia import done声明后,没有任何反应。

终端只是处于一种似乎正在等待发送更多数据或者给出更多指令的状态。它没有做任何事情。我必须按CTRL + C退出。

什么是终端等待,为什么一旦它从FB索引所有内容,它就不会自行退出?

1 个答案:

答案 0 :(得分:0)

我参加了Meetup,有人指出,如果我将新数据添加到我的Firebase数据库,那么它会在Algolia内自动编入索引。这意味着终端闲置的原因是因为它仍然在听事件。基本上它不应该停止停留,以便它可以不断搜索。