Ashutosh Maheshwari Ashutosh Maheshwari - 2 months ago 11
Javascript Question

How to know if data on firebase is saved before moving to next webpage?

I am making a signup page. Users enter their name, username, email, password for sign up. After they clicked sign up they are redirected to new page, but before moving that I want to make sure their entered data is saved in firebase database. How to make sure data is saved before moving on to next webpage?

function SignUP(){
const emailID = _email.value;
const pass = _password.value;
const auth = firebase.auth();
const promise = auth.createUserWithEmailAndPassword(emailID, pass);
promise.catch(e => console.log(e.message));
}
function writeUserData(user, fname, lname) {
var myuser = {
username: name,
firstName: fname,
lastName: lname
};
firebase.database().ref('/users').push(myuser);
window.open("chat.html",'_self',false);
}
firebase.auth().onAuthStateChanged(firebaseUser => {
if(firebaseUser){
console.log(firebaseUser);
const fName = _firstName.value;
const lName = _lastName.value;
const uName = _username.value;
writeUserData(uName, fName, lName);
}else{
console.log('not logged in');
}});

Answer

Wire up a .then to the push:

firebase.database().ref('/users').push(myuser).then(function(){
    //data is saved
});
Comments