[英]Error: The “path” argument must be of type string. Received undefined. firebase deploy --only functions
I am trying to deploy my firebase cloud functions.我正在尝试部署我的 firebase 云功能。 When I run
firebase deploy --only functions
after adding my functions to the index.js file it gives me the error mentioned above.当我在将我的函数添加到 index.js 文件后运行
firebase deploy --only functions
,它给了我上面提到的错误。
here is my firebase.json file:这是我的 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"
}
}
here is my index.js file:这是我的 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);
});
I tried running firebase deploy --only functions
with the hello world snippet they provide.我尝试使用他们提供的 hello world 代码段运行
firebase deploy --only functions
。 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.正如错误消息所说,您需要始终将路径传递给
functions.firestore.document(...)
函数,以确定函数在哪些文档路径上触发。
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:如果您还希望它们在用户文档上触发,就像
onCreate
,它们将是:
exports.updateIndex = functions.firestore.document('users/{userId}').onUpdate(change =>{
...
exports.updateIndex = functions.firestore.document('users/{userId}').onDelete(snapshot =>{
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.