So I am trying to make a post request via jquery and then console.log the data object in the callback function of the request. client-side:
var save = $('<button/>',{
class: 'btn btn-link',
id: url,
type: 'submit',
value: 'Submit',
text: 'save for later?',
click: function(event){
var reqData = { article : this.id};
$.post('/profile', reqData, function(data){
console.log(data);
})
}
server-side:
router.post('/profile', function(req, res) {
console.log(req.body);
// User.find();
console.log(req.user._id);
var article = new Article({article: req.body.article});
article.save(function(error, doc) {
if (error) {
res.send(error);
} else {
var id = doc._id;
User.findOneAndUpdate({
_id: req.user._id
}, {$push :{
article: id
}}).exec(function(err, doc) {
if (err) {
res.send(err);
} else {
res.send(doc);
}
})
res.send(doc);
}
})
I keep getting the error message 'can't set headers after they are sent', and if I comment out the res.send(doc); within the .exec promise, then the data will console.log, but the page will quickly refresh and erase it.