Rendolph
Rendolph

Reputation: 431

req ist not defined even when it's filled with values

Im updating some of my data inside my database with a REST API. Therefore I created a route with POST (I use it for both) like this and call it with a submit button:

router.post('/add/edit/update', function (req, res, next) {
  console.log(req.body);
  var sql = 'UPDATE gerichte SET gericht = ' + reg.body.gericht + ', Kalorien = ' + reg.body.Kalorien + ', preis_id = ' + req.body.preis + ', kategory_id = ' + req.body.kategorie + ', allergene = ' + req.body.allergene + ' WHERE g_id = ' + req.body.g_id + ';';
  conn.query(sql, function (err, data) {
    if (err) throw err;
  });
  res.redirect('/add/edit');  // redirect to user form page after inserting the data
});

But I always get the error req not found when I sumbit the form. I tried printing the req object but it's filled nicely:

[Object: null prototype] {
  g_id: '1',
  gericht: 'Schnitzel mit Pommes',
  Kalorien: '1400',
  preis: '1',
  kategorie: '5',
  allergene: 'dsd'
}

Any ideas how to fix this problem?

Upvotes: 0

Views: 44

Answers (2)

NullDev
NullDev

Reputation: 7303

It's req (with a lower-case Q), not reg (with lower-case G).


Change

var sql = 'UPDATE gerichte SET gericht = ' + reg.body.gericht + ', Kalorien = ' + reg.body.Kalorien + ', preis_id = ' + req.body.preis + ', kategory_id = ' + req.body.kategorie + ', allergene = ' + req.body.allergene + ' WHERE g_id = ' + req.body.g_id + ';';

to

var sql = 'UPDATE gerichte SET gericht = ' + req.body.gericht + ', Kalorien = ' + req.body.Kalorien + ', preis_id = ' + req.body.preis + ', kategory_id = ' + req.body.kategorie + ', allergene = ' + req.body.allergene + ' WHERE g_id = ' + req.body.g_id + ';';

Upvotes: 1

MoeezShahid
MoeezShahid

Reputation: 35

var sql = 'UPDATE gerichte SET gericht = ' + reg.body.gericht + ', Kalorien = ' + reg.body.Kalorien + ', preis_id = ' + req.body.preis + ', kategory_id = ' + req.body.kategorie + ', allergene = ' + req.body.allergene + ' WHERE g_id = ' + req.body.g_id + ';';

The error might be reg not defined because: In the above line of your code, you've been using reg.body, when there is no reg defined. Kindly correct it and see if the issue is resolved or not.

Upvotes: 0

Related Questions