ERROR:
ERROR SyntaxError: Unexpected end of JSON input
at JSON.parse (<anonymous>)
at Response.Body.json
LIVE:
https://voting-app-10.herokuapp.com/polls
QUESTION:
When I try to create a poll locally, everything works fine.
When deployed to heroku I get the error above.
What could be causing this ?
CODE:
Here is the project code: https://www.dropbox.com/s/6eop089d2dehm7p/Voting%20App.zip?dl=0
poll.service
addPoll(poll: Poll) {
const body = JSON.stringify(poll);
const token = localStorage.getItem('token')
? '?token=' + localStorage.getItem('token')
: '';
const headers = new Headers({
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + token
});
return this.http.post('https://voting-app-10.herokuapp.com/poll', body, {headers: headers})
.map((response: Response) => {
const result = response.json();
const poll = new Poll(
result.obj.title,
result.obj.choice1,
result.obj.choice2,
0,
0,
result.obj.user.firstName,
result.obj._id,
result.obj.user._id,
);
this.polls.unshift(poll);
return poll;
})
.catch((error: Response) => {
this.errorService.handleError(error.json());
return Observable.throw(error);
});
}
routes/poll
router.post('/', function (req, res, next) {
var decoded = jwt.decode(getToken(req));
User.findById(decoded.user._id, function (err, user) {
if (err) {
return res.status(500).json({
title: 'An error occurred',
error: err
});
}
var poll = new Poll({
title: req.body.title,
choice1: req.body.choice1,
choice2: req.body.choice2,
counter1: 0,
counter2: 0,
user: user
});
poll.save(function (err, result) {
if (err) {
console.log("ERROR: "+err);
return res.status(500).json({
title: 'An error occurred',
error: err
});
}
user.polls.push(result);
user.save();
res.status(201).json({
poll: 'Saved poll',
obj: result
});
});
});
});
via Coder1000
No comments:
Post a Comment