更新:这是通过更改我的Firebase数据库规则解决的:读取和写入到“true”
当我在终端中运行我的代码时,我得到零错误,但并不是所有的代码都被执行了? 这是怎么回事?这是我的代码,我也依赖于此代码,但它包含私有的凭据。
console.log('>>>Algolia Search Starting<<<');
//console.log('>>>0<<<');
console.log(' ');
var admin = require("firebase-admin");
admin.initializeApp({
credential: admin.credential.cert({
projectId: "...",
clientEmail: "...",
privateKey: "..."
}),
databaseURL: "..."
});
var firebase = require('firebase');
var config = {
apiKey: "myFirebaseAPIKey",
authDomain: "myFirbaseProjectID.firebaseapp.com",
databaseURL: "myFB_URL",
storageBucket: "myFirebaseStorageBucket"
};
firebase.initializeApp(config);
//Algolia Initialization
var algoliasearch = require('algoliasearch');
var client = algoliasearch('myAlgoliaApplicationID', 'myAlgoliaPrivateAPIKey');
var index = client.initIndex('meal');
var rootRef = firebase.database().ref('meal');
// Get all data from Firebase
rootRef.on('value', initIndex);
// Import Existing Data**
function initIndex(dataSnapshot) {
// Array of data to index
var objectsToIndex = [];
// Get all objects
var values = dataSnapshot.val();
// Process each Firebase ojbect
console.log('>>>0<<<');
for (var key in values) {
if (values.hasOwnProperty(key)) {
// Get current Firebase object
console.log('>>>1<<<');
var firebaseObject = values[key];
// Specify Algolia's objectID using the Firebase object key
firebaseObject.objectID = key;
console.log('>>>2<<<');
// Add object for indexing
objectsToIndex.push(firebaseObject);
console.log('>>>3<<<');
}
}
// Add or update new objects
index.saveObjects(objectsToIndex, function(err, content) {
if (err) {
throw err;
}
console.log('>>>4<<<');
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 = 'meal_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, 'meal', 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');
});
}
答案 0 :(得分:1)
Firebase数据库规则:读取和写入设置为null。我把它们改成了真的