-4

EDIT:

Thank you for your help ! You helped me pinpoint the problem and showed me the solution I was looking for: Accessing Express.js local variables in client side JavaScript


CODE:

index.ejs

$('.UpvoteButton').click(function () {
    if (authdata == null) {
        return res.redirect('/users/login');
    }
    else {
        var $this = $(this);
        var $other = $('.DownvoteButton');
        if ($this.hasClass("on")) {
            $this.removeClass("on");
        } else if (!$this.hasClass('on') && $other.hasClass("on")) {
            $this.addClass('on');
            $other.removeClass("on");
        } else {
            $this.addClass('on');
        } 
    } 
}); 

app.js

//Global vars
app.use(function(req, res, next) {
    res.locals.authdata = firebase.auth().currentUser;
    next();
});  

PROBLEM:

When I click, nothing happens. It worked before when I didn't have the if-else statement and the redirect statement.

N.B.: This is inside an EJS file. I am using Node.js.


QUESTION:

What have I done wrong ?

Community
  • 1
  • 1
Coder1000
  • 4,071
  • 9
  • 35
  • 84

1 Answers1

1

As stated, you're trying to access a server-sided variable on the client side. Check this out: Accessing EJS variable in Javascript logic.

Essentially, you'll want to declare the variable on the client side using:

<script>
    var authdata = <%= authdata %>            
</script>

Then, you can add your code. Remember to change the redirect to something that is recognized by the client ie. window.location.href = "/users/login";

Community
  • 1
  • 1
Quangdao Nguyen
  • 1,343
  • 11
  • 25