I'm having a problem parsing my JSON request using express, and after many hours of trying to research it, I can't figure out what the problem is. Here is my code snippet
app.post('/Information', (req, res)=> {
console.log(req.body); // this prints out the exact POST request from Postman
const info = JSON.parse(req.body); // this returns SyntaxError: Unexpected token o in JSON at position 1
let insertQuery = `insert into Information(id, name, number, message)
values(${info.id}, '${info.name}', '${info.number}', '${info.message}')`
client.query(insertQuery, (err, result)=>{
if(!err){
res.send('Insertion was successful')
}
else{ console.log(err.message) }
})
client.end;
})
I know the reason behind the parsing error, trying to parse an 'o', because req.body is an object, but I tried the three methods of accessing properties of an object, and I get 'undefined'. I don't know if this will help, but if I send this POST request { "id":"3", "name":"Mark Zuckerberg", "number":"1112223333", "message":"Welcome to Facebook!" } from Postman, then this is what console.log(req.body) will print out {'{\r\n "id":"3",\r\n "name":"Mark Zuckerberg",\r\n "number":"1112223333",\r\n "message":"Welcome to Facebook!"\r\n}\r\n': ''}. This output looks odd to me because of the (: '') at the end, as it makes it look like there are only 2 properties for some reason and I don't understand why that's happening. I would be glad to try out any ideas you guys have, because I'm really lost right now. Thank you in advance.