I am trying to post users who owns dogs in my database. in the registration form i handle the input when the user presses the submit button. The user is then posted to the database. I want to use the response to attach the userId to the dog (which is a list consisting of ownerId, name, etc..). The user is being posted to the database as far as i can see, however, this is the error that occurs: "index.js:1446 Error: SyntaxError: Unexpected end of JSON input"
if i change res.json() to res.text(), the response will be success " ".
handleSubmit() {
let user = {}
user.email = this.state.email
user.password = this.state.password
user.firstName = this.state.firstName
user.lastName = this.state.surName
user.dog = null;
console.log(user);
fetch('http://localhost:3001/rest/registration/', {
method: 'POST', // or 'PUT'
body: JSON.stringify(user), // data can be `string` or {object}!
headers: new Headers({
'Content-Type': 'application/json'
}),
})
.then(res => res.json())
.then(response => console.log('Success:', JSON.stringify(response)))
.catch(error => console.error('Error:', error));
};
i expect the response to be what the user put into the registration form with an additional unique Id.