1

I am trying to save a result of a query into a variable, I have read that you have to use a callback to do this. I am having problems with this process and receiving an error.

function getQryUser(user) {
  if (user.chkUserStatus) {
    var qryUser = request.query("SELECT someItems on someTbls", function(err, rows, done) {
      if (err) {
        return done(err, null);
      } else {
        return done(null, rows[0]);
      }
    });
  }
}

and from where I am calling my function

var qryUser = getQryUser(user);
Mike Cluck
  • 31,869
  • 13
  • 80
  • 91
crod
  • 235
  • 3
  • 5
  • 12
  • TypeError: done is not a function – crod Jan 24 '17 at 19:52
  • Pro-tip: Get into the habit of formatting your code correctly. It will help you and everyone else identify issues faster. I just formatted it for you so you can get an idea of what it should look like :) As for your problem, have you tried doing `console.log(done)` to see what `done` actually is? Does the documentation state that you should get a `done` callback here? Based on [this](https://github.com/Azure/node-sqlserver/blob/master/test/query.js) you should not be getting a `done` function. Just a possible error and the results. – Mike Cluck Jan 24 '17 at 19:54
  • If you're interested in getting the results from the query back from the function, you may want to check out [this question.](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Mike Cluck Jan 24 '17 at 19:57

1 Answers1

0

Assuming that you are using node-sqlserver, query() callback takes only two parameters, error and results. Here's an example from their official unit tests:

sql.query(conn_str, "SELECT 1 as X, 'ABC', 0x0123456789abcdef ", function (err, results) {
    assert.ifError(err);

    var buffer = new Buffer('0123456789abcdef', 'hex');
    var expected = [{ 'X': 1, 'Column1': 'ABC', 'Column2': buffer}];

    assert.deepEqual(results, expected, "Results don't match");
    ...

Did you perhaps look at the unit tests for examples yourself and picked up done() from there? Note that it comes from the parent suite() scope outside of query() and is thus not relevant in general query() API usage.

mrts
  • 16,697
  • 8
  • 89
  • 72