Rasul Imanov
Rasul Imanov

Reputation: 45

cannot retrieve data correctly from a MySQL table with Node.js

Here is the query I have to make in the browser:

/line?l1=##&l2=##&l3=##

And here is how I have it implemented with JS:

app.get('/line', (req, res) => {
  let sql = `UPDATE car_info SET
  l1 = ${parseInt(req.query.lineone)},
  l2 = ${parseInt(req.query.linetwo)},
  l3 = ${parseInt(req.query.linethree)}
  WHERE name = '${req.cookies.uName.name}'`;
  let lineQuerys = db.query(sql, (result) => {
    res.send(`l1: ${req.query.lineone}, l2: ${req.query.linetwo}, l3: ${req.query.linethree}`);
    })
});

the l1, l2, and l3 are as they are defined in my MySQL table. I keep getting this output when I type the query in the browser.

l1: undefined, l2: undefined, l3: undefined

Upvotes: 0

Views: 44

Answers (1)

Thang Duc
Thang Duc

Reputation: 326

Your url should be /line?lineone=##&linetwo=##&linethree=## because you're getting params by this: req.query.lineone

If you want to keep the short url, the query params should be: req.query.l1

app.get('/line', (req, res) => {
  let sql = `UPDATE car_info SET
  l1 = ${parseInt(req.query.l1)},
  l2 = ${parseInt(req.query.l2)},
  l3 = ${parseInt(req.query.l3)}
  WHERE name = '${req.cookies.uName.name}'`;
  let lineQuerys = db.query(sql, (result) => {
    res.send(`l1: ${req.query.l1}, l2: ${req.query.l2}, l3: ${req.query.l3}`);
  })
});

Upvotes: 2

Related Questions