0

I am trying to get the name, value field as cookies using loop. in my app.get method and then posting the field values in app.post method, I am using expressjs.

Can please anybody check the 'for loop' below and see my commented lines and brighten my knowledge on how to achieve the email field.

enter image description here Please also check my uploaded image for UI.

var cookies = [];
    if (req.cookies) {
        for (var cookieName in req.cookies) {
            var cookieValue = req.cookies[cookieName];
            // var cookieEmail = req.cookies[cookieValue]; //I dont see my email being posted to server
            cookies.push({ name: cookieName, value: cookieValue });
//  cookies.push({ name: cookieName, value: cookieValue, email: cookieEmail });
        }
    }
  • 1
    cookies have name and value not email. if you really want to send email and more attributes through cookie, you could add put them as json or comma separated or form url encoded) and encode with encodeURIComponent and set as value itself. – gp. Sep 30 '18 at 04:45
  • @gp my commented code in for loop, is it wrong? could you give me an example code.. – manojkarmocha Sep 30 '18 at 08:04
  • `cookieValue = encodeURIComponent( $.param({value: "", email: ""}) );` use this cookieValue. note: $.param is a jquery function. – gp. Oct 03 '18 at 05:45

1 Answers1

0

The req.cookies is a JSON object. When looping over A javascript object you have to be careful you do not grab prototype data. Take a look at this post here. https://stackoverflow.com/a/684692/10067782

Bdyce
  • 332
  • 2
  • 11