我正在使用 firebase auth 和實時資料庫創建一個注冊表單。
這是表單的基本布局(沒有 CSS)。
<script type="module">
// Import the functions you need from the SDKs you need
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.4.0/firebase-app.js";
import { getDatabase, set, ref, update , onValue } from "https://www.gstatic.com/firebasejs/9.4.0/firebase-database.js";
import { getAuth, createUserWithEmailAndPassword, signInWithEmailAndPassword, onAuthStateChanged, signOut , GoogleAuthProvider, signInWithRedirect, getRedirectResult, TwitterAuthProvider } from "https://www.gstatic.com/firebasejs/9.4.0/firebase-auth.js";
// Your web app's Firebase configuration
const firebaseConfig = {
//CONFIDENTIAL
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const database = getDatabase(app);
const auth = getAuth();
signUp.addEventListener('click',(e) => {
var email = document.getElementById('signUpEmail').value;
var password = document.getElementById('signUpPass').value;
// var username = document.getElementById('username').value;
createUserWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
// Signed in
const user = userCredential.user;
let dt = new Date();
set(ref(database, 'users/' user.uid),{
// username: username,
email: email,
prem: false,
pday: dt.getDate(),
pmon: dt.getMonth(),
pyear: dt.getFullYear()
})
alert('Signup Successful!');
window.close();
// window.location.replace('index.html');
})
.catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
alert(errorMessage);
// ..
});
});
<h1 class='status'>signup</h1>
<input type="text" class="email" name="email" id="signUpEmail" placeholder="E-mail" />
<input type="password" class="password" name="password" id="signUpPass" placeholder="Password" />
<input type="submit" id='signUp' name="signUp" value="signUp" />
身份驗證成功發生,但用戶詳細資訊未寫入實時資料庫。
我將以下內容存盤在資料庫中:
- 用戶電子郵件
- 如果他們有我的產品的高級訂閱 (T/F)
- 保費結束的那一天
- 保費結束的月份
- 保費結束的年份
我哪里錯了?
uj5u.com熱心網友回復:
將資料寫入資料庫(并從中讀取資料)是一種異步操作。這里的異步意味著您的主要代碼繼續執行,而操作在后臺運行。但這也意味著您window.close()的主代碼在資料庫寫入完成之前執行,實際上它似乎取消了該操作。
解決方案是在關閉視窗之前等待資料庫操作完成,類似于您已經執行的操作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();
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/419448.html
標籤:
