不使用回调时的未定义结果。 Nodejs、Express 和 SQL Server Express
Undefined result when not using callback. Nodejs, Express, and SQL Server Express
以下问题由 mssql、Nodejs、Gulp、Express 和 SQL Server Express 组成。我能够成功登录 SQL Server Express。但是,当我使用没有回调的bookRoute.js代码片段时,返回值是undefined。然而,当我使用回调时,我得到了数据。但是,我不明白为什么。
app.js 代码片段:
var config = {
user: 'user',
password: 'password',
server: 'localhost',
database: 'Books',
options: {
instance: 'SQLEXPRESS'
}
};
sql.connect(config, function(err){
console.log(err);
});
bookRoute.js 没有回调的代码片段:
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 带回调的代码片段:
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);
});
});
用户访问网页后,控制台应显示结果。不幸的是,当不使用回调时,唯一显示的结果是 undefined。
控制台输出:
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 ' } ]
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);
});
});
当使用不带 callback.The then
函数的 bookRoute.js 代码片段时,应该只有一个参数,即查询结果,即 documentation.当出现错误时,调用 catch
函数。
以下问题由 mssql、Nodejs、Gulp、Express 和 SQL Server Express 组成。我能够成功登录 SQL Server Express。但是,当我使用没有回调的bookRoute.js代码片段时,返回值是undefined。然而,当我使用回调时,我得到了数据。但是,我不明白为什么。
app.js 代码片段:
var config = {
user: 'user',
password: 'password',
server: 'localhost',
database: 'Books',
options: {
instance: 'SQLEXPRESS'
}
};
sql.connect(config, function(err){
console.log(err);
});
bookRoute.js 没有回调的代码片段:
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 带回调的代码片段:
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);
});
});
用户访问网页后,控制台应显示结果。不幸的是,当不使用回调时,唯一显示的结果是 undefined。
控制台输出:
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 ' } ]
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);
});
});
当使用不带 callback.The then
函数的 bookRoute.js 代码片段时,应该只有一个参数,即查询结果,即 documentation.当出现错误时,调用 catch
函数。