0

我被困@ express node js中的问题之一。我收到错误“未定义网址”

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

1 回答 1

1

您收到此错误是因为该url变量未在任何地方定义。

MySQL 请求的结果在回调的第二个参数中可用:

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

查看mysql 的文档以获取示例请求。

于 2013-11-20T12:08:35.407 回答