I am trying to deploy my firebase cloud functions. When I run firebase deploy --only functions
after adding my functions to the index.js file it gives me the error mentioned above.
here is my firebase.json file:
"functions": {
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint"
],
"source": "functions"
},
"database": {
"rules": "database.rules.json"
},
"firestore": {
"rules": "firestore.rules",
"indexes": "firestore.indexes.json"
}
}
here is my index.js file:
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);
});
I tried running firebase deploy --only functions
with the hello world snippet they provide. It worked fine with that.
As the error message says, you need to always pass a path to the functions.firestore.document(...)
function, to determine on which document paths the function triggers.
You do this correctly here:
exports.addToIndex = functions.firestore.document('users/{userId}').onCreate(snapshot=>{
...
But you are not passing a path in these two cases:
exports.updateIndex = functions.firestore.document().onUpdate(change =>{
...
exports.updateIndex = functions.firestore.document().onDelete(snapshot =>{
...
If you also want them to trigger on user documents, just like with onCreate
, they'd be:
exports.updateIndex = functions.firestore.document('users/{userId}').onUpdate(change =>{
...
exports.updateIndex = functions.firestore.document('users/{userId}').onDelete(snapshot =>{
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.