即使在侦听器功能(单击事件)中也有一些功能。在它的内部,我有一个函数,当单击“下订单”按钮时将数据发送到Firestore。我有一个.then函数,可在将数据发送到Firestore后向用户发出“您的订单已下达”警报。这很好用,但是当我在警报功能之后添加另一个.then函数以在警报窗口关闭后切换到另一个页面时,数据永远不会发送到firestore。
var placeOrderButton = document.getElementsByClassName('placeOrderBtn')
for (var i = 0; i < placeOrderButton.length; i++) {
var button = placeOrderButton[i]
button.addEventListener('click', placeOrderClicked)
}
function placeOrderClicked(event) {
var y = year.getFullYear();
var m = month.getMonth();
var d = day.getDate();
var h = hours.getHours();
var m = minutes.getMinutes();
var s = seconds.getSeconds();
var mm = milliseconds.getMilliseconds();
var orderID = ("" + y + m + d + h + m + s + mm);
localStorage.setItem('processedOrderID', orderID);
//getting and sending data to order
db.collection('shoppingSession').doc(localStorage.getItem('seshID')).collection('inCart').get().then(function (querySnapshot) {
querySnapshot.forEach(function (doc) {
var idName = doc.id;
var title = doc.data().title;
var qty = Number(doc.data().qty);
var price = Number(doc.data().price);
var itemTotal = qty * price;
var orderTotal = 90.00;
var thiss = orderTotal.toFixed(2);
addingPrices.push(itemTotal);
//moving data to orders table for cooks
db.collection('orders').doc(orderID).collection('inCart').add({
title: title,
price: price,
qty: qty
});
});
}).then(function () {
alert("Processing your order...");
}).then(function () {
location.assign("index.html");
});
// remember that this inside an onclick event
}; //end of onlick event