0

Below is the API that basically stores data in session if not already present then returns an empty JSON, otherwise sends the session data stored corresponding to that mail:

app.use('/session', function(req, res) {
  if (req.body.email in req.session) {
    console.log("user data already available");
    res.send(req.session[req.body.email]);
  } else {
    req.session[req.body.email] = {};
    req.session[req.body.email]["email"] = req.body.email;
    req.session[req.body.email]["gender"] = req.body.gender;
    req.session[req.body.email]["dbname"] = req.body.dbname;
    console.log("user session stored");
    res.send({});
  }
});

Below is the ajax call that I am making from my application. It is a cross domain call and I have set the response header accordingly.

$.ajax({
    url: 'url/session',
    type: "POST",
    crossOrigin: true,
    data: {
      "email": "email",
      "gender": "all",
      "dbname": "dbname"
    },
    success: function(data) {
      console.log("inside session api call success");
      console.log(data)
    }
    });
});

Now the problem is that if I hit the API using postman, it works fine but when I use that in my application, it overwrites the session every time and returns empty json. What am I doing wrong?

edit : Figured out that each time ajax call is happening, past session data gets deleted for all emails ids.session value gets reset but still unable to figure out why this is happening.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Srijan Sharma
  • 683
  • 1
  • 9
  • 19
  • 1
    Just add `dataType : 'json'` on ajax call and try – Santosh Suryawanshi Dec 05 '17 at 16:46
  • @SantoshSuryawanshi tried it but still the same problem. – Srijan Sharma Dec 05 '17 at 16:51
  • @SrijanSharma why don't you use an IDE? A breakpoint could answer this question for you immediately. In this day and age not using an IDE, especially with all of the lightweight options available for node is frankly stupid or uninformed. I suggest vs code since you don't have to do anything to set up the node debugger except press F5 and pick node – Aluan Haddad Dec 05 '17 at 16:54
  • your ajax call `URL parameter` `url: 'url/session',` is different form `app.use('/session',()) `function. just make them equal and see – Santosh Suryawanshi Dec 05 '17 at 17:03
  • @SantoshSuryawanshi I don't understand what you are trying to say. url basically species the host url on which my api is hosted. What do you mean by make them equal? – Srijan Sharma Dec 05 '17 at 17:07

0 Answers0