0
votes

Getting the following error:

"Cannot read property 'userName' of undefined at Promise.all.then.result"

Also Getting Error

"The behavior for Date objects stored in Firestore is going to change AND YOUR APP MAY BREAK. To hide this warning and ensure your app does not break, you need to add the following code to your app before calling any other Cloud Firestore methods:

 const firestore = new Firestore();
  const settings = {/* your settings... */ timestampsInSnapshots: true};
  firestore.settings(settings);

With this change, timestamps stored in Cloud Firestore will be read back as Firebase Timestamp objects instead of as system Date objects. So you will also need to update code expecting a Date to instead expect a Timestamp. For example:

 // Old:
  const date = snapshot.get('created_at');
  // New:
  const timestamp = snapshot.get('created_at');
  const date = timestamp.toDate();

Please audit all existing usages of Date when you enable the new behavior. In a future release, the behavior will change to the new behavior, so if you do not follow these steps, YOUR APP MAY BREAK."

However in my android project the place where i have defined the "Date" variable i have place the "@ServerTimestamp" on top.

Appreciate the help guys.

Code:

/*eslint-disable */

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

    exports.sendNotification = functions.firestore.document('notifications/{userEmail}/userNotifications/{notificationId}').onWrite((change, context) => {
const userEmail = context.params.userEmail;
const notificationId = context.params.notificationId;

    return admin.firestore().collection("notifications").doc(userEmail).collection("userNotifications").doc(notificationId).get().then(queryResult => {
        const senderUserEmail = queryResult.data().senderUserEmail;
        const notificationMessage = queryResult.data().notificationMessage;

        const fromUser = admin.firestore().collection("users").doc(senderUserEmail).get();
        const toUser = admin.firestore().collection("users").doc(userEmail).get();

        return Promise.all([fromUser, toUser]).then(result => {
            const fromUserName = result[0].data().userName;
            const toUserName = result[1].data().userName;
            const tokenId = result[1].data().tokenId;

            const notificationContent = {
                notification: {
                    title: fromUserName + " is shopping",
                    body: notificationMessage,
                    icon: "default"
                }
            };

            return admin.messaging().sendToDevice(tokenId, notificationContent).then(result => {
                console.log("Notification sent!");
                //admin.firestore().collection("notifications").doc(userEmail).collection("userNotifications").doc(notificationId).delete();
            });
        });
    });
});
1
What happens if you write to the log as follows: console.log(result[0].data()); console.log(result[1].data());?Renaud Tarnec
"Cannot read property 'userName' of undefined at Promise.all.then.result" Are you sure you have the exact userName property and not something else?Alex Mamo

1 Answers

0
votes

Make sure the document you're request actually exists. data() will return undefined if it doesn't. You can use the exists property on the resulting DataSnapshot to check if a document was actually found.