Firebase Cloud Function技术求助:Node.js编写时遇TypeError错误
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 inevent.params - Current (v1.0+) syntax:
onWrite((change, context) => { ... })where route params are incontext.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

