0

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.

ArnBry
  • 3
  • 4

1 Answers1

0

Your question seems to be answered Here.
Here's how I would change it reflecting off of that answer.

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((response) => {
      console.log(response);
      response.json()
        .then((data) => {
          console.log(data); //Change this to what you want do show
        });
     });
Mick Vader
  • 100
  • 9