Android端Firebase通知报错求助:TypeError: 无法读取null的'from'属性
Hey there, let's work through this error you've been stuck on! That TypeError: Cannot read property 'from' of null is telling us that at line 25 of your index.js, you're trying to access the from property of a value that came back as null. Specifically, this is happening when you're fetching the sender's user data after getting the notification snapshot.
Let's break down why this might be happening and how to fix it:
1. You're trying to read a property directly from a DataSnapshot (not its value)
Firebase's once('value') returns a DataSnapshot object, not the raw user data. If you're writing something like:
sender_id.then(fromUserResult => { const senderData = fromUserResult.from; // ❌ Wrong! fromUserResult is a snapshot, not the data })
That's going to fail because the snapshot object doesn't have a from property. You need to call .val() first to get the actual user data:
sender_id.then(fromUserResult => { const senderData = fromUserResult.val(); // ✅ Correct: get the raw user data from the snapshot })
2. The sender ID in your notification points to a non-existent user (or wrong path)
Even if you use .val(), if the user node for that sender ID doesn't exist, senderData will be null. Double-check:
- Is your user data stored under
/Users/{sender_id}(case matters! Firebase Database is case-sensitive—make sure it's not/usersorUser)? - Does the
fromfield in your/Notifications/{receiver_id}/{notification_id}node actually hold a valid, existing sender ID? Maybe it's empty or pointing to a deleted user.
3. You aren't handling null cases before accessing properties
Always add a check to make sure the data exists before reading its properties. Here's how to update your code to handle this gracefully:
'use strict' const functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.initializeApp(functions.config().firebase); exports.sendNotification = functions.database.ref('/Notifications/{receiver_id}/{notification_id}') .onCreate((snapshot, context) => { // First, get the notification data and validate the "from" field exists const notification = snapshot.val(); if (!notification || !notification.from) { console.error('Notification missing required "from" field'); return null; } const senderId = notification.from; // Fetch the sender's user data return admin.database().ref(`/Users/${senderId}`).once('value') .then(fromUserSnapshot => { const fromUser = fromUserSnapshot.val(); // Handle case where sender user doesn't exist if (!fromUser) { console.error(`No user found with sender ID: ${senderId}`); return null; } // Now safely access properties from fromUser const senderName = fromUser.name; // Replace with whatever field you need const payload = { notification: { title: `${senderName} sent you a notification`, body: notification.body, clickAction: 'FLUTTER_NOTIFICATION_CLICK' // Adjust based on your platform } }; // Send the notification to the receiver's token return admin.messaging().sendToDevice(notification.token, payload); }) .catch(error => { console.error('Error sending notification:', error); return null; }); });
4. Add debugging logs to narrow down the issue
If you're still stuck, add more console logs to see exactly what's coming back:
console.log('Notification data:', notification); console.log('Sender ID being fetched:', senderId); console.log('Fetched sender data:', fromUser);
This will help you confirm if the sender ID is valid, if the user path is correct, and if the data is actually present.
内容的提问来源于stack exchange,提问作者Manzini




