javascript - Node.js npm mssql function returning undefined - Stack Overflow

I am using mssql with node.js to connect to an sql server db. I am trying to reduce code by wrapping th

I am using mssql with node.js to connect to an sql server db. I am trying to reduce code by wrapping the connection code in a function with one query parameter. When I call the function from with in a router.get function, it returns undefined.

Any help would be much appreciated.

function sqlCall(query) {
  var connection = new sql.Connection(config, function(err) {
    if (err) {
      console.log("error1");
      return;
    }

    var request = new sql.Request(connection); // or: var request = connection.request();
    request.query(query, function(err, recordset) {
      if (err) {
        console.log("error2");
        return;
      }

      return (recordset);
    });
  });
}

I am using mssql with node.js to connect to an sql server db. I am trying to reduce code by wrapping the connection code in a function with one query parameter. When I call the function from with in a router.get function, it returns undefined.

Any help would be much appreciated.

function sqlCall(query) {
  var connection = new sql.Connection(config, function(err) {
    if (err) {
      console.log("error1");
      return;
    }

    var request = new sql.Request(connection); // or: var request = connection.request();
    request.query(query, function(err, recordset) {
      if (err) {
        console.log("error2");
        return;
      }

      return (recordset);
    });
  });
}

router code

router.get('/', function(req, res) {

  var queryString = "select * from .....";

  res.json(sqlCall(queryString));

  //sqlCall(queryString)

});

Share Improve this question edited Jan 20, 2015 at 10:13 Himmet Avsar 1,53116 silver badges23 bronze badges asked Jan 20, 2015 at 9:39 user1781272user1781272 9522 gold badges14 silver badges25 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

You are trying to treat the sqlCall as a synchronous function with a return value, while the request.query function on the opposite is an asynchronous function, expecting a callback.

Since Node.js uses non blocking IO and callback structures for flow control, using an asynchronous structure based around callbacks is the way to go. In your case this could look like this:

router.get('/', function(req, res) {


  var queryString = "selec * from .....";
  sqlCall(queryString, function(err, data) {
     if (typeof err !== "undefined" && err !== null) {
       res.status(500).send({
         error: err
       });
       return;
     }

     res.json(data);
  });
});

with your other ponent looking like this:

function sqlCall(query, cb) {
  var connection = new sql.Connection(config, function(err) {
    if (typeof err !== "undefined" && err !== null) {
      cb( err );
      return
    }

    var request = new sql.Request(connection); // or: var request = connection.request();
    request.query(query, function(err, recordset) {
      cb( err, recordset );
    });

  });

}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745106881a4611612.html

相关推荐

  • javascript - Node.js npm mssql function returning undefined - Stack Overflow

    I am using mssql with node.js to connect to an sql server db. I am trying to reduce code by wrapping th

    11小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信