The above code redirects me to the required page, but doesn't enter data into the database.
If I remove window.location.href
command, the data is successfully entered in the database.
I want to do both the task together. What is the way out?
var firebasep = firebase.database().ref();
var cool = firebasep.child(i.value).set(j.value);
console.log(cool);
firebasep.set(newData, function(error) {
alert("New");
window.location.href="Votingpage.html";
});
You can use promises as mentioned here to figure out when it's safe to redirect the user to Votingpage.html
firebasep.set(newData).then(function(data){
alert("New");
window.location.href="Votingpage.html";
}).catch(function(error){
console.log(error);
});
It redirects you to the required page but doesn't enter data into the database because the query failed but you mistakenly put your redirection code inside of the error callback, fix you code up like this to make it clearer to see what's happening
firebase.set(newData)
.then((data) => {
// redirect here
})
.catch((error) => {
// display error here
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.