3

when i make post request from postman, the request data was shown server but server response i.e status code and json object was not send to postman, postman was just loading... and after some time it says could not get response from the server.

here is my server.js class

var bodyParser = require('body-parser');
var express = require('express');

var {mongoose} = require('./db/mongoose');
var {Todo} = require('./models/todo');
var {Users} = require('./models/Users');

var app = express();
app.use(bodyParser.json());

app.post('/todos', (req, res) =>{
var todo = new Todo({
text: req.body.text
});

todo.save().then((doc) => {
res.send(doc);
}, (e) => {
res.status(400).send(e);
});

});

app.listen(3000, () =>{
console.log('started at port 3000');
});
malik
  • 41
  • 3

1 Answers1

0

There is some error in executing todo.save do console.log(e) to check what is the error throwing by mongo. Also you can send error in a response just to see what is going on, instead of writing res.status(400).send(e); write res.send(e) and it will send error as a response. "could not get response" error happens usually when you haven't send any response from the server. In your case you are sending response from your Promise resolved block but not from your error block.

Also make sure todo.save() return promise? may be you can achieve this by

todo.save(function(err,doc){
if(err){
return res.send(err);
}
res.send(doc);
})
Usman Hussain
  • 187
  • 1
  • 9