执行firebase deploy --only functions时报path参数类型错误如何解决
问题排查及解决方法
根因定位
报错The "path" argument must be of type string. Received undefined的直接触发原因是你编写的Cloud Functions代码存在两处语法错误:
- Firestore触发器的
document()方法必须传入要监听的文档路径参数,你编写的更新、删除事件触发器未传入该参数,导致path参数读取为undefined。 - 存在函数重名问题,更新触发、删除触发的两个函数都使用了
updateIndex作为导出名,后定义的函数会直接覆盖前者。
修复步骤
- 补全更新、删除触发器的
document()参数,统一设置为和新增触发器一致的users/{userId} - 将删除事件对应的导出函数名修改为唯一值,比如
deleteFromIndex - 部署前可先执行
npm --prefix functions run lint检查本地代码语法错误,提前规避问题
修正后的完整index.js代码
const functions = require('firebase-functions'); const algoliasearch = require('algoliasearch'); // 从Cloud Functions环境变量获取algolia的API密钥 const APP_ID = functions.config().algolia.app; const ADMIN_KEY = functions.config().algolia.key; // 初始化algolia客户端 const client = algoliasearch(APP_ID, ADMIN_KEY); const index = client.initIndex('pals'); // Firestore文档创建时同步新增到algolia索引 exports.addToIndex = functions.firestore.document('users/{userId}').onCreate(snapshot=>{ const data = snapshot.data(); const objectID = snapshot.id; return index.addObject({...data, objectID}); }); // Firestore文档更新时同步更新algolia索引 exports.updateIndex = functions.firestore.document('users/{userId}').onUpdate(change =>{ const newData = change.after.data(); const objectID = change.after.id; return index.saveObject({...newData, objectID}); }); // Firestore文档删除时同步删除algolia索引 exports.deleteFromIndex = functions.firestore.document('users/{userId}').onDelete(snapshot =>{ return index.deleteObject(snapshot.id); });
验证部署
代码修改完成后,重新执行部署命令即可正常完成部署:
firebase deploy --only functions
内容的提问来源于stack exchange,提问作者Deeptej
相关产品推荐
相关产品推荐

