Monday, 16 September 2019

How can I insert request body into a MySQL database using express js?

So i am trying to make a small api with MySQL and Express JS, after following a small tutorial online on how to use MySQL with Express i tried to make this api and send requests with postman, to insert the data i sent using postman i used in my code the 'req.body.data' syntax, i tried using tha classic SQL syntax of 'INSERT INTO table (col1, col2) VALUES (val1, val2)' using interpolation for the request body and i also tried the 'SET ?' as i saw in the tutorial i watched. When trying to send the request with postman i keep getting the "Cannot read property 'title' of undefined" error. How can i insert data that i send to the table with postman?app.get('/addtodo' , (req, res) => { let sql = 'INSERT INTO todo SET ?' let post = { title: req.body.title, body : req.body.body, date: req.body.date, importance: req.body.importance } db.query(sql, post, (err, res) => { if(err) throw err; console.log('success'); console.log(res); }); });

Submitted September 16, 2019 at 10:57AM by Vlad246

No comments:

Post a Comment