Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
-2
rated 0 times [  2] [ 4]  / answers: 1 / hits: 5841  / 5 Years ago, mon, september 16, 2019, 12:00:00

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 the 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?



JavaScript



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);
});
});

More From » mysql

 Answers
3

Use Post method instead get because you can not pass body in get method



app.post('/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
}
...
});

[#6236] Friday, September 13, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
latrelllloydb

Total Points: 449
Total Questions: 92
Total Answers: 100

Location: French Polynesia
Member since Tue, Jul 7, 2020
4 Years ago
latrelllloydb questions
Fri, Jul 10, 20, 00:00, 4 Years ago
Fri, May 29, 20, 00:00, 4 Years ago
Mon, Jun 17, 19, 00:00, 5 Years ago
;