Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
489 views
in Technique[技术] by (71.8m points)

javascript - Firebase TypeError: Cannot read property 'val' of undefined

I have tried Firebase cloud function for sending a notification.My project structure Database Structure

and this is the index.js,

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

exports.pushNotification = functions.database.ref('/messages').onWrite( event => {
console.log('Push notification event triggered');

    const message = event.data.val();
    const user = event.data.val();
    console.log(message);
    console.log(user);

    const topic = "myTopic";
    const payload = {
        "data": {
            "title": "New Message from " + user,
            "detail":message,
        }
    };

    return admin.messaging().sendToTopic(topic, payload);
   });

The above code is misconfigured, when I deploy in Node.js, LOG in Function shows:

"TypeError: Cannot read property 'val' of undefined".

What Actually I am trying to do : I am trying to extract info from snapshot load into that index.js so that when a new child gets added to Real-time database, it should trigger a notification payload with a title and body.

In Android, I use a child listener, for listening when a new record is added

FirebaseDatabase.getInstance().getReference().child("messages")

 OnChildAdded(.....){
 if (dataSnapshot != null) {
                    MessageModel messageModel = dataSnapshot.getValue(MessageModel.class);
                    if (messageModel != null) {
                            // do whatever
                           }
                   }

But in index.js, I could not able to parse that. A bit guidance how to fixate index.js according to my database structure would be immensely appreciated. PS- I have never done coding in JS If you want more context, I'd be happy to provide it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...