我可以根据视频指南here成功部署HelloWord应用程序。
但现在我按照说明here将Algolia和Firebase与云功能相关联,我可以在我的计算机上运行代码并进行更新,但不知何故,当我进行更改时它不会自动更新在Firebase上。
其次,当我部署应用程序时,我得到了"部署完成"消息但Firebase中没有任何内容>>当我对Firebase进行更改时,功能和Algolia未更新。
index.js代码
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.load();
// 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);
//synchronize firebase database with algolia index
const contactsRef = database.ref('/contactDetail/botswana');
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(contact) {
// Get Algolia's objectID from the Firebase object key
const objectID = contact.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);
});
}

package.json代码
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"serve": "firebase serve --only functions",
"shell": "firebase experimental:functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"dependencies": {
"algoliasearch": "^3.24.9",
"dotenv": "^4.0.0",
"firebase": "^4.8.1",
"firebase-admin": "~5.4.2",
"firebase-functions": "^0.7.1"
},
"private": true
}

答案 0 :(得分:0)
您是否检查了Functions
部分中的日志?要查看Algolia中的更改,您应该首先看到firebase控制台中显示的功能。
答案 1 :(得分:0)
我必须在代码运行之前对代码进行一些更改
函数不使用.on()作为侦听器而是使用例如onCreate()有一个参数,更多here。见下文
exports.childAdded=contactsRef.onCreate(addOrUpdateIndexRecord);
解决方案:
const algoliasearch = require('algoliasearch');
const dotenv = require('dotenv');
const functions = require('firebase-functions');
// load values from the .env file in this directory into process.env
dotenv.load();
// configure algolia
const algolia = algoliasearch(
process.env.ALGOLIA_APP_ID,
process.env.ALGOLIA_API_KEY
);
const index = algolia.initIndex(process.env.ALGOLIA_INDEX_NAME);
//synchronize firebase database with algolia index
const contactsRef = functions.database.ref('/contactDetail/locationA/{contactID}');
exports.childAdded=contactsRef.onCreate(addOrUpdateIndexRecord);
exports.childChanged=contactsRef.onUpdate(addOrUpdateIndexRecord);
exports.childRemoved=contactsRef.onDelete(deleteIndexRecord);
function addOrUpdateIndexRecord(contact) {
console.log("The function addOrUpdateIndexRecord is running!");
// Get Firebase object
const record = contact.data.val();
// Specify Algolia's objectID using the Firebase object key
record.objectID = contact.data.key;
// Add or update object
return 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(contact) {
// Get Algolia's objectID from the Firebase object key
const objectID = contact.data.key;
// Remove the object from Algolia
return 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);
});
}