我正在尝试部署我的 firebase 云功能。当我在将函数添加到 index.js 文件后运行 firebase deploy --only functions
时,它给了我上面提到的错误。
这是我的 firebase.json 文件:
"functions": {
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint"
],
"source": "functions"
},
"database": {
"rules": "database.rules.json"
},
"firestore": {
"rules": "firestore.rules",
"indexes": "firestore.indexes.json"
}
}
这是我的 index.js 文件:
const functions = require('firebase-functions');
const algoliasearch = require('algoliasearch');
// get api keys for the algolia from the env variable of cloud functions
const APP_ID = functions.config().algolia.app;
const ADMIN_KEY = functions.config().algolia.key;
// algolia client
const client = algoliasearch(APP_ID, ADMIN_KEY);
const index = client.initIndex('pals');
// ADD algolia index from firestore database when a document is created in firestore:
exports.addToIndex = functions.firestore.document('users/{userId}').onCreate(snapshot=>{
const data = snapshot.data();
const objectID = snapshot.id;
// add objectID to algolia index
return index.addObject({...data, objectID});
});
// UPDATE algolia index from firestore database when a document is updated in firestore
exports.updateIndex = functions.firestore.document().onUpdate(change =>{
// change.after gives the document after the change
const newData = change.after.data();
const objectID = change.id;
// update objectID to algolia index
return index.saveObject({...newData, objectID});
});
// DELETE algolia index from firestore database when a document is deleted in firestore
exports.updateIndex = functions.firestore.document().onDelete(snapshot =>{
// delete objectID to algolia index
return index.deleteObject(snapshot.id);
});
我尝试使用他们提供的 hello world 代码段运行 firebase deploy --only functions
。它工作得很好。
答案 0 :(得分:0)
正如错误消息所说,您需要始终将路径传递给 functions.firestore.document(...)
函数,以确定函数在哪些文档路径上触发。
您在此处正确执行此操作:
exports.addToIndex = functions.firestore.document('users/{userId}').onCreate(snapshot=>{
...
但是在这两种情况下您没有传递路径:
exports.updateIndex = functions.firestore.document().onUpdate(change =>{
...
exports.updateIndex = functions.firestore.document().onDelete(snapshot =>{
...
如果您还希望它们在用户文档上触发,就像 onCreate
一样,它们将是:
exports.updateIndex = functions.firestore.document('users/{userId}').onUpdate(change =>{
...
exports.updateIndex = functions.firestore.document('users/{userId}').onDelete(snapshot =>{
...