尝试使用 JavaScript 和 jQuery 以及 Firebase 电子邮件/密码身份验证创建基本的登录和注册表单。
但是我在控制台@这一行收到此错误“Reference.push失败:第二个参数必须是有效函数”
database.ref().push(emailnew, passwordnew);
我一直在 stackoverflow 和 firebase 文档中查看如何解决此问题,但我却空手而归。我还觉得我的 JS 文件可能包含不必要的内容,但我不知道如何简化它。截至目前,没有任何内容被推送到数据库。我对 firebase 并不完全熟悉,所以任何添加的提示都会非常有帮助,谢谢。
理想情况下,成功创建或登录新用户帐户后,此启动页面会将用户重定向到实际的应用程序页面。
这是 HTML 表单
注册
<div class="field-wrap">
<label>
Name<span class="req">*</span>
</label>
<input type="name" id="name" required autocomplete="off"/>
</div>
<div class="field-wrap">
<label>
Email Address<span class="req">*</span>
</label>
<input type="email-signup" id="emailSignup" required autocomplete="off"/>
</div>
<div class="field-wrap">
<label>
Set A Password<span class="req">*</span>
</label>
<input type="password-signup" id="passwordSignup" required autocomplete="off"/>
</div>
<button type="" id="signupbutton" />Get Started</button>
</div>
<!-- User Login Form -->
<div id="login">
<h1>Welcome</h1>
<div class="field-wrap">
<label>
Email Address<span class="req"></span>
</label>
<input type="emailLogin" id="emailLogin" required autocomplete="off"/>
</div>
<div class="field-wrap">
<label>
Password<span class="req"></span>
</label>
<input type="passwordLogin" id="passwordLogin" required autocomplete="off"/>
</div>
<p class="forgot"><a href="#">Forgot Password?</a></p>
<button class="button button-block" id="login" />Log In</button>
<div id=loginmessage>
</div>
这是我与 firebase 集成的 user.js 文件,如下所示:
// Initialize Firebase
var config = {
apiKey: 'AIzaSyBU1fYqhQrVskqgA0Okr3ZStPfYz0s3QWQ',
authDomain: "https://lackluster-5966e.firebaseapp.com",
databaseURL: 'https://lackluster-5966e.firebaseio.com',
projectId: "lackluster-5966e",
storageBucket: 'https://lackluster-5966e.appspot.com',
messagingSenderId: "489067404953"
};
firebase.initializeApp(config);
// var firebaseref = new Firebase("https://lackluster-5966e.firebaseapp.com");
var database = firebase.database();
var auth = firebase.auth();
var user = firebase.auth().currentUser;
console.log(config);
var emailnew;
var passwordnew;
//Collect User Data from Signup
$("#signupbutton").click(function(event){
event.preventDefault();
emailnew = $("#emailSignup").val();
passwordnew = $("#passwordSignup").val();
database.ref().push(emailnew, passwordnew); //Error occurs
console.log(emailnew);
console.log(passwordnew);
});
//Creates New User via Firebase Authentication
var promise = auth.createUserWithEmailAndPassword(emailnew, passwordnew);
promise.then(function(user) {
user.sendEmailVerification().then(function() {
// Email sent.
}, function(error) {
// An error happened.
});
//Sends User Info to firebase DB
user.updateProfile({
Name: name,
Email: emailnew
}).then(function() {
// Update successful.
}, function(error) {
// An error happened.
});
// Clears all of the text-boxes for user signup
$("#emailSignup").val("");
$("#passwordSignup").val("");
//User Login Event
var emailLogin = document.getElementById('emailLogin');
var passwordLogin = document.getElementById('passwordLogin');
$("#login").click(function(event){
event.preventDefault();
var email = emailLogin.value;
var password = passwordLogin.value;
var auth = firebase.auth();
var promise = auth.signInWithEmailAndPassword(email, password);
promise.catch(function (e) {
return console.log(e.message);
});
// //Page redirect
// firebase.auth().onAuthStateChanged(user => {
// if(user) {
// window.location = 'index.html';
// }
// else{
// //Do nothing.
// }
// });
// Authentication Listner
// Verifies that login credentials are correct otherwise returns error message
var Message = "<div class=\"loginmessage\">" + "Login Unsuccessful" + "</div>";
firebase.auth().onAuthStateChanged(function (firebaseUser) {
if (firebaseUser) {
console.log(firebaseUser);
} else {
$('#loginmessage').append(Message);
console.log('not logged in');
} // end else statement
}); // end function
});
// Clears all of the text-boxes for user login
$("#emailLogin").val("");
$("#passwordLogin").val("")
});
最佳答案
push
方法需要一个对象作为参数。
修改您对此的调用:
// the property names are up to you
database.ref().push({ email: emailnew, password: passwordnew });
关于javascript - 使用firebase的用户注册和登录错误 "Reference.push failed: second argument must be a valid function",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46801039/