Amir A.Eitan Amir A.Eitan - 1 month ago 14
Node.js Question

res.send doesn't print to console

I have an ajax call:

$.ajax({
method: "POST",
url: "http://localhost:3000/",
dataType: "json",
data: JSON.stringify({"start" : a.rangeInfos[0].startMarkerId,
"end" : a.rangeInfos[0].endMarkerId}),
contentType: "application/json; charset=utf-8",
success: function(result) {
console.log(result);
}
});


that is being handled on the server like:

app.post('/', function(req, res){
res.send('Got a POST request');
database.doSomething(req);
});


doSomething is defined in the connection.js, as:

doSomething : function(req) {
console.log(req.body);
db.collection('ranges').insert(req.body)
}


Everything works great except that the result is not being printed out.
It did print out before I made some changes to the connection and server files, so I'm suspecting it's not a front-end issue, but some weird async issue that keeps escaping me.

Would appreciate help.

Lep Lep
Answer

In ajax you declared

dataType: "json"

that means you're expecting data back from the server under json format. So the string Got a POST request && saved To DB cannot be parsed