TypeError: from_data.data is not a function

By | July 17, 2018
Questions:

I’m working on a project “Android Firebase Push Notifications – Device to Device”

I’m having a problem with FireBase function when sending a message from my device to another device but I get this error

error 3 TypeError: from_data.data is not a function
at admin.firestore.collection.doc.collection.doc.get.then.queryResult (/user_code/index.js:25:58)
at process._tickDomainCallback (internal/process/next_tick.js:135:7)

and The notification does not appear in the other device

code index.js

"use-strict"

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

admin.initializeApp();

exports.sendNotification = functions.firestore
.document("Users/{user_id}/Notification/{notification_id}").onWrite((data, context) => {

    const user_id = context.params.user_id;//sender id
    const notification_id = context.params.notification_id;//notification id
    console.log("user id : (" + user_id+ ") | notification id : (" + notification_id+ ")");


    return admin.firestore()
        .collection("Users").doc(user_id)
        .collection("Notification").doc(notification_id)
        .get().then(queryResult => {

const from_user_id = queryResult.data().from;

            const from_data = admin.firestore().collection("Users").doc(from_user_id).get();//the sender object in the database
            const to_data = admin.firestore().collection("Users").doc(user_id).get();//now this is also the sender id
            console.log("we come here 1" + from_data.data().name);
            //const message_body = admin.firestore().collection("Users").doc(user_id).collection("Notification").doc(notification_id).from;
            console.log("we come here 2");
            return Promise.all([from_data, to_data]).then(result => {
                console.log("do we come here 3");
                const from_name = result[0].data().name;
                 const to_name = result[1].data().name;
                 const token_id = result[1].data().tokenId;

                console.log("from name: ");
                const payload = {
                    notification: {
                        title: "Notification From: ",
                        body: "from_message should be replaced with the real message coming from the android device",
                        icon: "default"
                    },
                    topic: "oop"
                };

                return admin.messaging().sendToDevice(token_id, payload)
                    .then((result) => {
                        console.log("Notification Sent.");
                        console.log("topic: " + from_topic);
                        return result;
                    })
                    .catch((error) => {
                        console.log("error 1",error);
                    });
            }).catch((error) => {
                        console.log("error 2",error);
                    });
        })
        .catch((error)=> {
            console.log("error 3",error);
        });
});
Answers:

Leave a Reply

Your email address will not be published. Required fields are marked *