部署firebase.auth.user().onCreate时遇TypeError:无法读取undefined的'user'属性
Firebase Auth用户创建触发器部署报错:TypeError: Cannot read properties of undefined (reading 'user')
部署Firebase Auth用户创建触发器函数时,遇到以下错误:TypeError: Cannot read properties of undefined (reading 'user')
触发器代码
exports.createExample = functions.auth.user().onCreate((testUser) => { functions.logger.log("inside the auth trigger ", testUser.uid); return null; });
相关情况说明
- 该触发器原本在生产环境可正常工作(当前代码为排查问题的简化示例)
- 已升级所有依赖:firebase-functions v6.3.2,firebase-admin v13.2.0
- 引入语句确认正确:
const functions = require("firebase-functions"); const admin = require("firebase-admin"); - 曾尝试将
admin.initializeApp()移入触发器函数内,错误依旧 - Firebase CLI版本为13.35.1,错误由部署阶段CLI抛出
- 疑惑部署阶段触发器为何被调用(本应仅在生产环境创建用户时触发)
完整部署错误日志
=== Deploying to 'sample_firebase_project'... i deploying functions Running command: npm --prefix "$RESOURCE_DIR" run lint > lint > eslint . ✔ functions: Finished running predeploy script. i functions: preparing codebase default for deployment i functions: preparing codebase fix_overwrite_issue for deployment i functions: ensuring required API cloudfunctions.googleapis.com is enabled... i functions: ensuring required API cloudbuild.googleapis.com is enabled... i artifactregistry: ensuring required API artifactregistry.googleapis.com is enabled... ✔ artifactregistry: required API artifactregistry.googleapis.com is enabled ✔ functions: required API cloudbuild.googleapis.com is enabled ✔ functions: required API cloudfunctions.googleapis.com is enabled ⚠ functions: Runtime Node.js 18 will be deprecated on 2025-04-30 and will be decommissioned on 2025-10-31, after which you will not be able to deploy without upgrading. Consider upgrading now to avoid disruption. See https://cloud.google.com/functions/docs/runtime-support for full details on the lifecycle policy i functions: Loading and analyzing source code for codebase default to determine what to deploy Serving at port 8689 TypeError: Cannot read properties of undefined (reading 'user') at Object.<anonymous> (/Users/test/AndroidStudioProjects/app/node_modules/firebase-tools/functions/index.js:35:47) at Module._compile (node:internal/modules/cjs/loader:1159:14) at Module._extensions..js (node:internal/modules/cjs/loader:1213:10) at Module.load (node:internal/modules/cjs/loader:1037:32) at Module._load (node:internal/modules/cjs/loader:878:12) at Module.require (node:internal/modules/cjs/loader:1061:19) at require (node:internal/modules/cjs/helpers:103:18) at loadModule (/Users/test/AndroidStudioProjects/app/node_modules/firebase-tools/functions/node_modules/firebase-functions/lib/runtime/loader.js:40:16) at loadStack (/Users/test/AndroidStudioProjects/app/node_modules/firebase-tools/functions/node_modules/firebase-functions/lib/runtime/loader.js:157:23) at /Users/testAndroidStudioProjects/app/node_modules/firebase-tools/functions/node_modules/firebase-functions/lib/bin/firebase-functions.js:56:56
注:loader.js和firebase-functions.js均为最新版本。
解决思路
版本兼容性排查
检查firebase-cli与firebase-functions的版本匹配度,尝试升级firebase-cli到最新稳定版,或降级firebase-functions至与当前CLI兼容的版本。清理并重装依赖
删除node_modules文件夹和锁文件(package-lock.json/yarn.lock),重新执行npm install,排除依赖缓存导致的版本冲突。检查codebase配置
日志显示存在两个codebase,确认firebase.json中的functions配置是否正确,避免不同codebase之间的依赖或配置冲突。规范admin初始化
在函数文件顶部全局执行admin.initializeApp(),不要将初始化逻辑放在触发器函数内部,防止重复初始化引发异常。排除本地环境干扰
在全新的临时目录中搭建项目并尝试部署,验证是否为本地环境的缓存或配置问题导致的报错。
内容的提问来源于stack exchange,提问作者stroopwafel
相关产品推荐
相关产品推荐

