phil o.O
phil o.O

Reputation: 416

Undefined result when not using callback. Nodejs, Express, and SQL Server Express

The following issue consists of mssql, Nodejs, Gulp, Express, and SQL Server Express. I am able to login to SQL Server Express successfully. However, the returned value is undefined when I use the bookRoute.js code snippet without callback. Yet, when I use a callback I get the data. However, I don't understand why.

app.js code snippet:

var config = {
    user: 'user',
    password: 'password',
    server: 'localhost',
    database: 'Books',
    options: {
       instance: 'SQLEXPRESS'
    }
};

sql.connect(config, function(err){
    console.log(err);
});

bookRoute.js code snippet without callback:

bookRouter.route('/')
.get(function (req, res) {
    console.log('book router');
    var request = new sql.Request();
    request.query('select * from books').then(
        function (err, recordset) {
            console.log(recordset);
        })
    .catch(function(err){  console.log(err)});
    });

bookRoute.js code snippet with callback:

bookRouter.route('/')
.get(function (req, res) {
    console.log('book router');
    var request = new sql.Request();
    request.query('select * from books',
        function (err, recordset) {
            console.log(recordset);
        });
});

Once a user accesses the webpage, then the console should display the results. Unfortunately, the only result that is shown is undefined when not using a callback.

console output:

P:\ub\lic\library>gulp serve
[11:08:28] Using gulpfile P:\ub\lic\library\gulpfile.js
[11:08:28] Starting 'style'...
[11:08:28] Starting 'inject'...
[11:08:53] Finished 'inject' after 808 ms
[11:08:53] Finished 'style' after 25 s
[11:08:53] Starting 'serve'...
[11:08:53] Finished 'serve' after 5.31 ms
[11:08:53] [nodemon] 1.9.2

[11:08:53] [nodemon] to restart at any time, enter `rs`
[11:08:53] [nodemon] watching: *.js src/**/*.js
[11:08:53] [nodemon] starting `node app.js`

running server on port 3000
null
book router
undefined
[11:09:21] [nodemon] restarting due to changes...
Restarting the server.....beep boop beep beep
[11:09:21] [nodemon] restarting due to changes...
Restarting the server.....beep boop beep beep
[11:09:21] [nodemon] starting `node app.js`
running server on port 3000
null
book router
[ { id: 1,
    title: 'A,B,C with Big Bird           ',
    author: 'Michael Jacob ' },
  { id: 2,
    title: 'Peter and his Petunias        ',
    author: 'Jess Holiday  ' },
  { id: 3,
    title: 'The Amazing Average Guy       ',
    author: 'Don Dillon    ' } ]

Upvotes: 2

Views: 624

Answers (1)

user6361120
user6361120

Reputation:

bookRoute.js code snippet without callback:

bookRouter.route('/').get(function (req, res) {
console.log('book router');
var request = new sql.Request();
request.query('select * from books')
    .then(function (recordset) {
        console.log(recordset);
    })
    .catch(function (err) {
        console.log(err);
    });
});

When using bookRoute.js code snippet without callback.The then function should have only one argument that is the result from the query that is how it is stated in the documentation.When there is an error the catch function is called.

Upvotes: 2

Related Questions