我正在制作一个注册页面。用户输入他们的姓名,用户名,电子邮件,密码进行注册。点击注册后,他们会被重定向到新页面,但在移动之前,我想确保他们输入的数据保存在Firebase数据库中。在进入下一个网页之前,如何确保数据已保存?
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');
}});
答案 0 :(得分:2)
将.then
连接到推送:
firebase.database().ref('/users').push(myuser).then(function(){
//data is saved
});