【问题标题】:Javascript functions in firebasefirebase 中的 Javascript 函数
【发布时间】:2019-02-24 18:23:42
【问题描述】:

得到以下错误:

"无法读取未定义的属性 'userName' 在 Promise.all.then.result"

也报错

“存储在 Firestore 中的 Date 对象的行为将发生变化 您的应用程序可能会中断。 要隐藏此警告并确保您的应用不会中断,您需要添加 在调用任何其他 Cloud Firestore 方法之前,将以下代码添加到您的应用中:

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

进行此更改后,存储在 Cloud Firestore 中的时间戳将被读回为 Firebase Timestamp 对象而不是系统 Date 对象。所以你也会 需要更新期望日期的代码,而不是期望时间戳。例如:

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

启用新行为时,请审核 Date 的所有现有用法。在一个 未来的版本,行为将更改为新的行为,所以如果你不 请按照以下步骤操作,您的应用程序可能会中断。”

但是,在我的 android 项目中,我定义了“日期”变量的地方,我将“@ServerTimestamp”放在了顶部。

感谢帮助的家伙。

代码:

/*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();
            });
        });
    });
});

【问题讨论】:

  • 如果按如下方式写入日志会发生什么情况:console.log(result[0].data()); console.log(result[1].data());?
  • "Cannot read property 'userName' of undefined at Promise.all.then.result" 你确定你有确切的userName 属性而不是别的吗?

标签: javascript firebase google-cloud-firestore


【解决方案1】:

确保您请求的文档确实存在。如果没有,data() 将返回 undefined。您可以使用生成的 DataSnapshot 上的 exists 属性来检查是否确实找到了文档。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-18
    • 2017-11-02
    • 1970-01-01
    • 2018-10-19
    • 2020-02-12
    • 1970-01-01
    相关资源
    最近更新 更多