gpt4 book ai didi

javascript - 使用 createUserWithEmailAndPassword 后 Firebase 无法看到用户

转载 作者:行者123 更新时间:2023-11-28 03:24:35 26 4
gpt4 key购买 nike

我是 firebase 的新手,我创建了一个非常简单的应用程序来注册用户。注册后,该用户未显示在我的 firebase 控制台上的用户列表中。我遵循了 Youtube 上的文档和一些教程。我觉得我做了同样的事情,但它不起作用。谁能帮我解决这个问题吗?

<!DOCTYPE html>
<html>
<head>
<title>Happy Referrals - Sign Up Free!</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="../css/style.css">
</head>
<body>
<div class="signup-container">
<form>
<h3>Sign Up Happy Referrals for Free!</h3>
<label>Email:</label>
<input type="email" name="" id="email" />
<label>Password:</label>
<input type="password" name="" id="pw" />
<button id="signup">Sign Up</button>
</form>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<!-- The core Firebase JS SDK is always required and must be listed first -->
<script src="https://www.gstatic.com/firebasejs/7.2.2/firebase-app.js"></script>
<!-- TODO: Add SDKs for Firebase products that you want to use
https://firebase.google.com/docs/web/setup#available-libraries -->
<script src="https://www.gstatic.com/firebasejs/7.2.3/firebase-auth.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.2.3/firebase-firestore.js"></script>

<script>
// Your web app's Firebase configuration
var firebaseConfig = {
// keep secret
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);

$('#signup').on('click', function() {
var email = $('#email').val();
var pw = $('#pw').val();
console.log(email);

firebase.auth().createUserWithEmailAndPassword(email, pw).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
console.log(errorMessage);
});
})

</script>
</body>
</html>

最佳答案

正如您将在 the W3 specification 中读到的那样您的按钮的默认行为是提交。因此,您的表单是在触发 Firebase 方法之前提交的。

因此您需要显式指定按钮类型,如下所示:

<button type="button" id="signup">Sign Up</button>
<小时/>

另外,请注意,使用 JQUery 时最好等待文档“准备好”。请参阅doc ,这表示:

A page can't be manipulated safely until the document is "ready." jQuery detects this state of readiness for you. Code included inside $( document ).ready() will only run once the page Document Object Model (DOM) is ready for JavaScript code to execute.

因此,您可以按如下方式调整您的脚本代码:

<script>

$( document ).ready(function() {
// Your web app's Firebase configuration
var firebaseConfig = {
// keep secret
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);

$('#signup').on('click', function() {
var email = $('#email').val();
var pw = $('#pw').val();
console.log(email);

firebase.auth().createUserWithEmailAndPassword(email, pw).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
console.log(errorMessage);
});
});

});

</script>

关于javascript - 使用 createUserWithEmailAndPassword 后 Firebase 无法看到用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58758452/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com