I'm trying to set cookie using res.cookie like below:
res.cookie('userId',req.user._id); //set cookie here
console.log(req.user._id); //returned correct value, eg abc
then I'm seeing j:"abc" in my cookie, why does this happens?
I know this is a bit late, but I came across this issue myself and have been digging around a bit. It seems they're prefixing any JSON strings with "j:" so they know it's a JSON string when parsing it back. What this basically means is that you have to manually remove the "j:" if you're using some other way of parsing it.
According the the Express 4 docs, res.cookie(name, value [, options])
sets a cookie name to a value. The value parameter may be a string or object converted to JSON.
In this instance, req.user._id
is an object so you would set the cookie as res.cookie('userId', JSON.stringify(req.user._id))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With