Javascript 为什么即使我定义了函数,也会出现引用错误?

Javascript 为什么即使我定义了函数,也会出现引用错误?,javascript,node.js,Javascript,Node.js,我有下面将要执行的代码,但在调用函数“Auth”的按钮上单击,我得到一个未定义的错误。为什么呢?我确实在下面的代码中定义了它,不是吗 app.get('/sign-in', function(req, res, next) { res.render('sign-in', { title: 'Sign In' }) function Auth(event) { event.preventDefault() firebaseAuth.signInWithEmai

我有下面将要执行的代码,但在调用函数“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)
    })
  }
})
它叫这里(帕格):

已编辑

(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)
})
})      

问题是,您在express服务器上定义的函数在客户端上不起作用,这就是它现在的工作方式

  • 您需要在express应用程序上创建一个Auth函数,该函数将尝试登录firebase
  • 创建一个端点,当通过POST/GET方法调用/访问该端点时,该端点将运行Auth函数
  • 然后在客户端,创建一个函数,在单击按钮时对服务器(express)端点执行ajax调用
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):


什么是没有定义的?函数Auth或其中的某个函数?在哪里调用Auth?我格式化了代码,没有看到它。这个按钮是在哪里定义的?函数在其作用域之外不可见,您在
app.get
函数的作用域内定义了此函数。代码不足,无法提供帮助。按钮单击处理程序的脚本在哪里?它没有定义,您已经在请求回调的范围内定义了它,还调用了res.render,然后定义了它。把它移到上面的范围。我可能也错了,但您将客户端和服务器端JavaScript混合在一起,并希望通过单击按钮就可以工作?非常感谢,但我得到了events.js(throw er;//未处理的“error”事件)。错误在哪里?你能用你的代码更新你的问题吗?只是想看看错误在哪里。在我看来,您仍在试图在服务器上运行客户端代码。@kupak12我已经用一些小补丁更新了代码。您在
events.js
中提到的错误来自节点端,如果没有可行的示例,很难调试。我现在没有收到错误。但是,当我单击按钮时,控制台中没有显示任何内容。有什么提示吗?您没有任何东西来处理成功的呼叫,我建议您通读有关如何处理
firebase.Promise
返回的
app.post('/api/sign-in', function(req, res, next) {
  firebase.auth().signInWithEmailAndPassword(req.query.username, req.query.password)
  .catch(function(error) {
    console.log(error);
  });
});
$('#button').on('click', function(event) {
  event.preventDefault();
  $.ajax({
    url: '/api/sign-in',
    method: 'POST',
    data: {
      username: $('#user').val(),
      password: $('#pass').val()
    }
  });
}