You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Firebase Cloud Function技术求助:Node.js编写时遇TypeError错误

Fix: TypeError: Cannot read property 'user_id' of undefined in Firebase Cloud Function

Hey Alex, let's break down why you're hitting this error and how to fix it—it's a common pitfall with Firebase Functions versions or tiny code typos:

1. Check Firebase Functions Version (Most Likely Cause)

If you're using Firebase Functions v1.0 or newer, the onWrite trigger's parameter structure changed significantly from older versions:

  • Legacy (v0.x) syntax: onWrite((event) => { ... }) where route params live in event.params
  • Current (v1.0+) syntax: onWrite((change, context) => { ... }) where route params are in context.params

Your code uses the legacy event.params approach, but if your firebase-functions dependency is updated, event.params will be undefined—which is exactly what's throwing your error.

Here's the corrected code for modern Firebase Functions:

exports.FindRoom = functions.database.ref(`/FindRoom/{user_id}`).onWrite((change, context) => { 
  // Grab user_id from context.params instead of event.params
  const user_id = context.params.user_id; 
  console.log("user_id", user_id); 
  // Use `change` to access database values (before/after the write)
  const user_question = change.after.val().question; // Adjust based on your actual data structure
  // ... rest of your function logic
});

2. Fix Obvious Typo in Your Code

Looking at your snippet, you have var user_question = even...—that's missing a t (should be event if you were using legacy syntax). While this isn't causing your current error, it'll break things later, so be sure to fix that too.

3. Verify Your Database Path Trigger

Double-check that your function is triggered by a path that actually includes the user_id parameter (e.g., /FindRoom/abc123 instead of just /FindRoom). If the path doesn't match the {user_id} wildcard, the function won't fire at all, but it's worth confirming just in case.

To confirm your dependency version, run this in your project directory:

npm list firebase-functions

内容的提问来源于stack exchange,提问作者Alex L

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 07:43:12