我想在我的網頁上創建一個「創建用戶」功能。在創建用戶時,它同時存儲用戶寫入的數據,例如名稱等。在完成此操作後,用戶應該只爲用戶提供一個頁面,問題出在我的代碼之前,用戶的數據(名稱等) )被存儲,用戶正在被呈現新的頁面。我怎樣才能防止這種情況發生?創建用戶,保存數據,然後改變頁面
我創建用戶功能:
firebase.auth().createUserWithEmailAndPassword(email, pass).then(function() {
// User is created
const imageUrl = "images/genericProfilePicture.png";
const admin = false;
const currentTime = timeStamp();
var user = firebase.auth().currentUser;
const userId = user.uid;
firebase.database().ref('users/' + userId).set({
name: name,
email: email,
address: address,
zip: zip,
city: city,
department: department,
membership: membership,
admin: admin,
added: currentTime,
profile_picture : imageUrl
});
window.open('frontpage.php', '_top');
}).catch(function(error) {
// Error has happened
var errorCode = error.code;
var errorMessage = error.message;
alert(errorMessage);
});
嘗試這種代碼沒有任何的運氣,沒有存儲數據,並沒有新的頁面顯示:
firebase.database().ref('users/' + userId).set({
\t \t \t \t \t \t \t \t name: name,
\t \t \t \t \t \t \t \t email: email,
\t \t \t \t \t \t \t \t address: address,
\t \t \t \t \t \t \t \t zip: zip,
\t \t \t \t \t \t \t \t city: city,
\t \t \t \t \t \t \t \t department: department,
\t \t \t \t \t \t \t \t membership: membership,
\t \t \t \t \t \t \t \t admin: admin,
\t \t \t \t \t \t \t \t added: currentTime,
\t \t \t \t \t \t \t \t profile_picture : imageUrl
\t \t \t \t \t \t \t }).then(() => {
\t \t \t \t \t \t window.open('frontpage.php', '_top');
\t \t \t \t \t \t });
但我已經在創建用戶功能。使用「然後」? – FeReTu
是的,但沒有什麼能阻止你嵌套承諾。 (很明顯,如果你嵌套太多,它會變得很討厭,但你只有兩個)。 – Roope
我編輯了試圖用代碼嘗試使用另一個的問題,但沒有存儲數據,也沒有顯示新的頁面? – FeReTu