0
我跟着這些Algolia/Firebase docs。我做了一些調整,因爲一些最初的設置說明已經過時了,但我得到了它的工作。我將所有來自該頁面的代碼放入我的app.js文件中。在終端內部,我運行'npm start',當我檢查了Algolia控制檯時,Indices成功索引了我的Firebase數據。Algolia/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
正如我所說的對象被索引(I只有內部火力地堡1個對象)。
問題出在最後的Firebase<>Algolia import done
聲明之後,沒有任何反應。
終端只是坐在一個狀態,似乎它正在等待更多的數據發送或更多的指令給予。它什麼都不做。我必須按CTRL + C才能退出。
什麼是終端等待,爲什麼不自行退出,一旦它索引從FB的一切?