在我的网站中将数据保存到 firebase 后无法重定向到另一个 html 文件
Cannot redirect to another html file after saving data to firebase in my web
我想向数据库添加一些会话存储值,然后将页面重定向到另一个 html 文件。但是当我插入重定向代码时,数据没有保存到 firebase,但是当我评论代码以重定向页面时,数据正按预期存储在 firebase 中。这些数据存储在 firebase 中后,如何重定向到另一个 html 文件?
函数 bookingConfirm(){
var id = 4;
var firebaseRootRef = firebase.database().ref().child("Orders");
var firebaseIdRef = firebaseRootRef.child(id);
firebaseIdRef.child("id").set(id);
firebaseIdRef.child("user").set(sessionStorage.getItem("userName"));
firebaseIdRef.child("movie").set(sessionStorage.getItem("movie"));
firebaseIdRef.child("date").set(sessionStorage.getItem("date"));
firebaseIdRef.child("seats").set(sessionStorage.getItem("seats"));
firebaseIdRef.child("snacks").set(sessionStorage.getItem("snacks"));
firebaseIdRef.child("ticket price").set(sessionStorage.getItem("total"));
firebaseIdRef.child("snack price").set(sessionStorage.getItem("snackPrice"));
firebaseIdRef.child("total price").set(sessionStorage.getItem("finalPrice"));
alert(" Your booking is done! ");
// location.href = "main.html";
// document.getElementById("quithref").setAttribute("href","main.html");
}
添加完成回调,如下所示:
firebaseIdRef.child("total price").set("I'm writing data", function(error) {
if (error) {
alert("Data could not be saved." + error);
} else {
alert("Data saved successfully.");
}
});
来自文档:https://firebase.google.com/docs/database/admin/save-data#section-completion-callback
我想向数据库添加一些会话存储值,然后将页面重定向到另一个 html 文件。但是当我插入重定向代码时,数据没有保存到 firebase,但是当我评论代码以重定向页面时,数据正按预期存储在 firebase 中。这些数据存储在 firebase 中后,如何重定向到另一个 html 文件?
函数 bookingConfirm(){
var id = 4;
var firebaseRootRef = firebase.database().ref().child("Orders");
var firebaseIdRef = firebaseRootRef.child(id);
firebaseIdRef.child("id").set(id);
firebaseIdRef.child("user").set(sessionStorage.getItem("userName"));
firebaseIdRef.child("movie").set(sessionStorage.getItem("movie"));
firebaseIdRef.child("date").set(sessionStorage.getItem("date"));
firebaseIdRef.child("seats").set(sessionStorage.getItem("seats"));
firebaseIdRef.child("snacks").set(sessionStorage.getItem("snacks"));
firebaseIdRef.child("ticket price").set(sessionStorage.getItem("total"));
firebaseIdRef.child("snack price").set(sessionStorage.getItem("snackPrice"));
firebaseIdRef.child("total price").set(sessionStorage.getItem("finalPrice"));
alert(" Your booking is done! ");
// location.href = "main.html";
// document.getElementById("quithref").setAttribute("href","main.html");
}
添加完成回调,如下所示:
firebaseIdRef.child("total price").set("I'm writing data", function(error) {
if (error) {
alert("Data could not be saved." + error);
} else {
alert("Data saved successfully.");
}
});
来自文档:https://firebase.google.com/docs/database/admin/save-data#section-completion-callback