1

I saw the data in express api reference

expressjs api reference for cookie

in the document, cookie can send as JSON res.cookie('cart', { items: [1,2,3] });

so I started to try, cookie worked well when I use string, but not in JSON format.

   res.cookie('cookietmp',{test: ['test1', 'test2']}, { maxAge: 900000, httpOnly: true});
   res.send('test cookie: ' + req.cookies.cookietmp)

this is my code

and my browser display

   test cookie: [object Object]

it seems like my browser doesn't know the format is JSON or something, how can I solve it?

LiJung
  • 7,649
  • 6
  • 27
  • 30

2 Answers2

0

That's an object literal, not JSON. JSON is a serialization format, but what you'retrying to set as the cookie value is not a string. You see '[object Object]' in the browser because that's what Object.toString returns.

You, the progammer, need to convert that object into JSON using JSON.stringify:

var cookieValue = JSON.stringify({test: ['test1', 'test2']}, { maxAge: 900000, httpOnly: true});
res.cookie('cookietmp', cookieValue);
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • but the example in expressjs document, you don't need to JSON. stringify before you set cookie, why? – LiJung Sep 03 '12 at 05:03
  • `res.send('test cookie: ' + req.cookies.cookietmp)` does an internal `req.cookies.cookietmp.toString()` resulting in a `[object Object]`. You don't need to serialize the cookie, the res.cookie method is smart enough to handle serialization and deserialization. try to send `res.send('test cookie: ' + JSON.stringify(req.cookies.cookietmp))` – LoG Sep 03 '12 at 06:10
  • now, I use `res.send('test cookie: ' + JSON.stringify(req.cookies.cookietmp))` I get `test cookie: "[object Object]"`, it isn't right.. – LiJung Sep 03 '12 at 09:24
  • @LiJung the object is being automatically coerced to a string as soon as you try to store it as a cookie. Of course `JSON.stringify(req.cookies.cookietmp)` isn't going to do anything useful because it's too late if you're trying to get the original object back out of `req.cookies.cookietmp`. You must JSON-stringify the object before you ever set it as a cookie. – Matt Ball Sep 03 '12 at 12:42
  • In the document, [see document here](http://expressjs.com/api.html#res.cookie) it seems I don't need to stringify JSON before I set. I know stringify will work but I wish to send cookie just in JSON, how can I do that?( Sorry for the wrong link in the question, I will fix it later.) – LiJung Sep 03 '12 at 12:55
  • @LiJung ah, it seems that you're right then. My mistake. Use your browser's dev tools to see the actual value that `cookietmp` is set to. It's probably correct, and it's just your "debugging" method (`res.send('test cookie: ' + req.cookies.cookietmp)`) is not the right way to check the value. Or, [**use a real debugger**](http://stackoverflow.com/questions/1911015/how-to-debug-node-js-applications). – Matt Ball Sep 03 '12 at 13:01
0

Your cookie is getting set appropriately. The problem is that you are setting a cookie on the response object, and then checking the stale request object for the value of the cookie. Updating the response does not update the incoming request:

console.log(req.cookies.cookieTmp)  // '[object Object]'

res.cookie('cookietmp',{test: ['test1', 'test2']}, { maxAge: 900000, httpOnly: true});
res.send('test cookie: ' + req.cookies.cookietmp)

console.log(req.cookies.cookieTmp)  // '[object Object]'
console.log(res.get('Cookie'))  // 'cookieTmp={test: ['test1', 'test2']}` (or urlencoded version of this).
YPCrumble
  • 26,610
  • 23
  • 107
  • 172