1

During login, I save the userId in a jQuery cookie, so that I can access it by $.cookie('userId').

Now that I am trying to access it from Jade, as,

- if ($.cookie('userId') === userId)

I get an error

$ is not defined

How can I access the cookie ?

My code:

- if (($.cookie('userId')) === userId)
                                                input(type='submit', value='Move')
                                            else
                                                input(type='submit', disabled, value='Move'')
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user644745
  • 5,673
  • 9
  • 54
  • 80

2 Answers2

14

You seem to misunderstand concepts. Let me give you a lecture. ;)

Basics. Cookie is stored on the client-side. So using jQuery $.cookie('userId') will retrieve it when this script is run on the client-side.

But here you use JADE templating engine on server-side. So you didn't even send the page to the client-side yet. So what cookie do you want to retrieve? There is no cookie, because you are at the server.

There's more. Running $.cookie('userId') throws the error, because you are on the server-side. There is no $ object. And there cannot be, because you cannot use jQuery on server-side (well, actually you can, but this is irrelevant at the moment). Did you define $ object in your route system? Or did you define helper $? I don't think so.

Express+JADE tutorial. Now let me give you a short tutorial. Say you have a view (in app.js)

app.get('/', function(req, res){
    res.render('index.jade', { code: 500 });
});

Now in index.jade you can use code as a variable. So you can write your index.jade for example like this:

// some code
- if (code == 500)
    input(type='submit', disabledm, value='Move')
- else
    input(type='submit', value='Move')
// some code

and now it will work! You may also use JavaScript functions in template if you predefine them. For example (in your app.js)

app.helpers({
    positive: function(no) { return no > 0; }
});

then in index.jade template you can use

- if (positive(-13))
    input(type="submit")

and even combine these two concepts

- if (positive(code))
    // do some stuff

To cookie or not to cookie. If you really need to use cookies (and you shouldn't except for sessions or CSRF for example) then you need to use client-side JavaScript. This means that you need to add (after jQuery)

script(src='my-path/my-script.js')

to the index.jade file and write my-script.js like this

$(document).ready(function() {
  if ($.cookie('userId') == userId) {
    // do something like append input to some holder
  }
});

But there are several problems with that solution. First: what is userId (second part of equality)? You need to predefine it in your index.jade. For example using

script
    var userId = #{ user.id };

and adding user object in your route. Second: cookies are local variables. Setting them requires additional server code which I do not thing you would like. This makes maintaining the app harder. And finally: storing a user's id in a cookie seems to be pointless. The final user don't even have to know it's own id. The server needs to know! Sessions system comes handy here.

Final note. The code you wrote shows us that you cannot distinguish between server-side and client-side JavaScript. The solution to your problem is to learn, learn and learn. Read tutorials and everything about JADE, Express and Node.js.

freakish
  • 54,167
  • 9
  • 132
  • 169
  • Thanks for all the points you mentioned. You are very right, I have messed up between client side and server side... That was the root problem. Thanks for pointing this out... great ! – user644745 Feb 27 '12 at 03:13
1

What @freakish said is right. You're trying server side vs. client side.
If the cookie is set you should be able to access it on server like so

app.use(express.cookieParser());

app.get('/', function(req, res){
   // index is jade template
   // local variable on jade template holds cookie value
   // use req.cookies.userid
   res.render('index', { userid: req.cookies.userid });

});
Cris-O
  • 4,989
  • 3
  • 17
  • 11