我是javascript新手,每个then()都出错。我想在数据库发生更改时发送通知。
index.js
'use strict'
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification = functions.firestore.document("Buyers/{buyerID}/Notifications/{notificationId}").onWrite(event => {
const buyerID = event.params.buyerID;
const notificationId = event.params.notificationId;
return admin.firestore().collection("Buyers").doc(buyerID).collection("Notifications").doc(notificationId).get().then(queryResult => {
const notificationMessage = queryResult.data().notificationMessage;
const notificationTitle = queryResult.data().notificationTitle;
return Promise.all([fromUser, toUser]).then(result => {
const tokenId = result[0].data().tokenId;
const notificationContent = {
notification: {
title: notificationTitle,
body: notificationMessage,
icon: "default",
sound : "default"
}
};
return admin.messaging().sendToDevice(tokenId, notificationContent).then(result => {
console.log("Notification sent!");
});
});
})
});