I'm trying a POST fetch request to my api server for react and no matter how i send the data, i keep getting Unexpected token o in JSON at position 1.
handleSubmit:
event.preventDefault();
fetch('http://localhost:3001/user/register',{
method:'POST',
headers:{
'Content-Type':'application/json'
},
body:this.state
})
.then(data => data.json())
.then(data =>{
if(data.success) console.log('It works');
else console.log(data.info + data.success);
console.log(this.state);
});
And my api app.post:
app.post('/user/register', (req, res)=>{
res.setHeader('Content-Type', 'application/json');
console.log('Commiting to register');
console.log(req.body);
let user = new userModel({
firstName:req.body.firstName,
lastName:req.body.lastName,
age:req.body.age,
email:req.body.email,
username:req.body.username,
password:req.body.password
});
user.save()
.then((info)=>{
let data = {success:true, info:info};
res.send(data);
})
.catch((err)=>{
let data = {success:false, info:err};
res.send(data);
});
console.log('Registering processed');
})