Firebase Cloud Functions 上的函数不 appear/run
Functions do not appear/run on Firebase Cloud Functions
我可以按照视频指南成功部署 HelloWord 应用程序 here。
但现在我按照说明 here 来 link Algolia 和 Firebase with Cloud Functions,我可以 运行 我电脑上的代码并更新,但不知何故当我在 Firebase 上进行更改时它不会自动更新。
其次,当我部署应用程序时,我收到 "deploy complete" 消息,但 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
}
您是否检查了 Functions
部分中的日志?为了看到 Algolia 中的变化,您应该首先看到该函数出现在您的 firebase 控制台中。
在代码运行之前,我不得不对其进行一些更改
- 函数需要使用 "exports"
导出
函数不使用 .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);
});
}
我可以按照视频指南成功部署 HelloWord 应用程序 here。
但现在我按照说明 here 来 link Algolia 和 Firebase with Cloud Functions,我可以 运行 我电脑上的代码并更新,但不知何故当我在 Firebase 上进行更改时它不会自动更新。
其次,当我部署应用程序时,我收到 "deploy complete" 消息,但 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
}
您是否检查了 Functions
部分中的日志?为了看到 Algolia 中的变化,您应该首先看到该函数出现在您的 firebase 控制台中。
在代码运行之前,我不得不对其进行一些更改
- 函数需要使用 "exports" 导出
函数不使用 .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);
});
}