Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

272
Views
Why do I get a reference error even though I defined the function?

I have the following code that would execute, but on a button click that calls function 'Auth', I get an error that it is not defined. Why is that? I did define it in the below code, did I not?

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

It's called here (pug):

 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)
})
})      
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Here is the problem, the functions you define on your express server won't work on the client, that's now how it works.

  • You need to create an Auth function on your express app that will try to login to firebase.
  • Create an endpoint that when called/accessed via POST/GET method will run the Auth function.
  • Then on the client side, create a function that will do an ajax call to your server (express) endpoint when the button is clicked.

e.g.

express code:

app.post('/api/sign-in', function(req, res, next) {
  firebase.auth().signInWithEmailAndPassword(req.query.username, req.query.password)
  .catch(function(error) {
    console.log(error);
  });
});

client code (using jQuery for clarity):

$('#button').on('click', function(event) {
  event.preventDefault();
  $.ajax({
    url: '/api/sign-in',
    method: 'POST',
    data: {
      username: $('#user').val(),
      password: $('#pass').val()
    }
  });
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!