I'm getting an
Unhandled Rejection (SyntaxError): Unexpected end of JSON input
error when I press submit.
this is my code:
onButtonSubmit = () => {
this.setState({imageUrl: this.state.input})
app.models
.predict(
Clarifai.FACE_DETECT_MODEL,
this.state.input)
.then(response => {
if (response) {
fetch('http://localhost:3000/image', {
method: 'put',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
id: this.state.user.id
})
}).then((response) => response.json())
.then((count) => {
this.setState(Object.assign(this.state.user, {entries: count}));
})
}
this.displayFaceBox(this.calculateFaceLocation(response))
})
.catch(err => console.log(err));
}
I new to React.js and I want to learn more, but I can't figure out why I get this error, and it goes through because the database gets updated. And when I login again, the page is also updated.
the error I get
Unhandled Rejection (SyntaxError): Unexpected end of JSON input
(anonymous function)
C:/Users/cmham/Projekter/facedetector/src/App.js:94
91 | body: JSON.stringify({
92 | id: this.state.user.id
93 | })
> 94 | }).then((response) => response.json())
| ^ 95 | .then((count) => {
96 | this.setState(Object.assign(this.state.user, {entries: count}));
97 | })
View compiled
how do I resolve this?
Edit my server part
app.put('/image', (req, res) => {
const { id } = req.body;
db('users').where('id', '=', id)
.increment('entries', 1)
.returning('entries')
.then(entries => {
res.json(entries[0]);
})
.catch(err => res.status(400).json('unable to get entries'))
})
Edit 2 Now I receive a 1 from the server but what I need is the updated number from the database.
this is my server now:
app.put('/image', (req, res) => {
const { id } = req.body;
db('users').where('id', '=', id)
.increment('entries')
.returning('entries')
.then(entries => {
res.json(entries);
})
.catch(err => res.status(400).json('unable to get entries'))
})
and this is my App.js:
onButtonSubmit = () => {
this.setState({imageUrl: this.state.input})
app.models
.predict(
Clarifai.FACE_DETECT_MODEL,
this.state.input)
.then(response => {
if (response) {
fetch('http://localhost:3000/image', {
method: 'put',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
id: this.state.user.id
})
}).then((response) => response.json())
.then((count) => {
console.log(count)
this.setState({
...this.state.user, entries: count++
});
})
.catch(error => console.log('An error occured ', error))
}
this.displayFaceBox(this.calculateFaceLocation(response))
})
.catch(err => console.log(err));
}
I don't get the number from the database, but I no longer get an error
{id:this.state.user.id}.entries? Can you post the json response from the server?.then()will execute, but fail on this line(response) => response.json().