Node JS-函数返回未定义而不是SQL结果

时间:2020-02-24 13:55:19

标签: javascript sql node.js db2

我正在尝试从函数获取值,但未定义为返回值...

function get_data(id){
  var name;
  ibmdb.open(dbcon.con, function (err,conn) {
    conn.query("select name from users where id='"+id+"'", function (err, data) {
      name = data[0].NAME;
      console.log(name) // there is the name is correct
      conn.close(function () {});
    });
  });
  return name; // there getting undefined
};
router.get('/:id', function(req, res, next) {
    res.render('u', { name: get_data(req.params.id) });
});

0 个答案:

没有答案