0

I am stuck @ one of the problem in express node js. I am getting error "url not defined"

 app.get("/id", function(req, res) {
 var id = req.param("id");
  connection.query('SELECT `url` FROM dynamic_url where id ='+req.param("id"), function          (error, rows, fields) {
 //how do i get the values of url here
res.send("value of url is " + url);
});
});
Paul Mougel
  • 16,728
  • 6
  • 57
  • 64
user2834795
  • 4,333
  • 2
  • 15
  • 10

1 Answers1

1

You're getting this error because the url variable isn't defined anywhere.

The results of the MySQL request are available in the second parameter of the callback:

app.get("/id", function(req, res, next) {
  var id = req.param("id");
  connection.query('SELECT `url` FROM dynamic_url where id ='+req.param("id"), function (error, rows, fields) {
    if (err) {
      return next(err);
    }
    var url;
    if (rows.length === 0) {
      url = 'URL not available in database'
    } else {
      url =  rows[0].url;
    }
    res.send("value of url is " + url);
  });
});

Check mysql's documentation for a sample request.

Paul Mougel
  • 16,728
  • 6
  • 57
  • 64