出现以下错误:
“无法读取未定义的属性'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时间戳对象,而不是作为系统日期对象。所以你也会 需要更新期望日期的代码而不是期望时间戳的代码。例如:
// Old: const date = snapshot.get('created_at'); // New: const timestamp = snapshot.get('created_at'); const date = timestamp.toDate();
启用新行为时,请审核Date的所有现有用法。在一个 在将来的版本中,该行为将更改为新行为,因此如果您不这样做 请按照以下步骤操作,您的应用程序可能会崩溃。“
但是,在我的android项目中,我定义“ Date”变量的位置将“ @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();
});
});
});
});