我有以下代码可以执行,但是在按钮单击调用函数'Auth'时,我收到一个未定义的错误。这是为什么?我确实在下面的代码中定义了它,不是吗?
app.get('/sign-in', function(req, res, next) {
res.render('sign-in', {
title: 'Sign In'
})
function Auth(event) {
event.preventDefault()
firebaseAuth.signInWithEmailAndPassword('email@email.com', 'password').catch(function(error) {
var errorCode = error.code
var errorMessage = error.message
console.log(errorCode)
console.log(errorMessage)
})
}
})
这里叫(帕格):
h2 Please sign in
form(class='form-signin' role='form')
input#email.form-control(type='email' placeholder='E-mail address' required='' autofocus='')
input#password.form-control(type='password' placeholder='Password' required='')
button.btn.btn-lg.btn-primary.btn-block(type='submit' onclick='Auth(event)') Sign In
EDITED :
(pug file)
link(rel='script', href='../public/javascripts/scripts.js')
body
.container
h2 Please sign in
form(class='form-signin' role='form')
input#email.form-control(type='email' placeholder='E-mail address' required='' autofocus='')
input#password.form-control(type='password' placeholder='Password' required='')
button.btn.btn-lg.btn-primary.btn-block(type='submit') Sign In
(public/javascripts/scripts.js)
$('button').on('click', function(event) {
event.preventDefault();
$.ajax({
url: '/api/sign-in',
method: 'POST',
data: {
email: $('#email').val(),
password: $('#password').val()
}
});
}
(app.js)
function Auth(email, password) {
return firebaseAuth.signInWithEmailAndPassword(email, password)
}
app.get('sign-in', function(req,res,next) {
res.render('sign-in', { title: 'Sign In' })
})
app.get('/api/sign-in', function(req, res, next) {
Auth(req.body.email, req.body.password)
.then(function() {
console.log('success!')
})
.catch(function(error) {
console.log(error)
})
})
答案 0 :(得分:0)
问题在于,您在快速服务器上定义的功能无法在客户端上运行,现在它的工作原理。
e.g。
快递代码:
app.post('/api/sign-in', function(req, res, next) {
firebase.auth().signInWithEmailAndPassword(req.query.username, req.query.password)
.catch(function(error) {
console.log(error);
});
});
客户端代码(为清晰起见使用jQuery):
$('#button').on('click', function(event) {
event.preventDefault();
$.ajax({
url: '/api/sign-in',
method: 'POST',
data: {
username: $('#user').val(),
password: $('#pass').val()
}
});
}