Я получаю
Необработанный отказ (SyntaxError): неожиданный конец ввода JSON
при нажатии кнопки подтверждения.
thisмой код:
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));
}
Мой app.js
Я новичок в React.js и хочу узнать больше, ноЯ не могу понять, почему я получаю эту ошибку, и она проходит, потому что база данных обновляется.И когда я снова захожу, страница тоже обновляется.
ошибка, которую я получаю
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
как мне решить эту проблему?
Редактировать мою серверную часть
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'))
})
Редактировать 2 Теперь я получаю 1 от сервера, но мне нужен обновленный номер из базы данных.
Теперь это мой сервер:
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'))
})
иэто мой 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));
}
Я не получаю номер из базы данных, но больше не получаю сообщение об ошибке