Unable to write to firebase realtime database even though authentication is successful
Solution 1:
Writing data to the database (and reading data from it) is an asynchronous operation. Asynchronous here means that your main code continues to execute, while the operations runs in the background. But this also means that your window.close()
in the main code executes before the database write completes, and in fact it seems to cancel that operation.
The solution is to wait for the database operation to complete before closing the window, similar to what you already do for createUserWithEmailAndPassword
:
set(ref(database, 'users/' + user.uid),{
// username: username,
email: email,
prem: false,
pday: dt.getDate(),
pmon: dt.getMonth(),
pyear: dt.getFullYear()
}).then(() => { // 👈
alert('Signup Successful!');
window.close();
})