获取 MySQL 结果到路由文件
Getting the MySQL results to the route file
所以有了一个名为 mobile.js
的文件,我创建了一个到数据库的连接,其中有一个调用查询的函数和 returns 一组手机:
var mysql = require('mysql');
var pool = mysql.createPool({
//database information
});
module.exports =
{
getAllModels: function()
{
pool.getConnection( function( err, connection )
{
connection.query("SELECT model FROM product", function( err, res, fie)
{
if( err ) throw err;
connection.release();
//console.log(res);
return res;
});
});
}
};
现在,取消注释上面的代码我确实得到了一个 JSON 对象 [{model: 'LG'}, {model: 'Samsung'}, ...]
,但是当我尝试通过以下方式访问 index.js
路由文件中的那个变量时:
var express = require('express');
var router = express.Router();
var mobileRepo = require('../repositories/mobile');
router.get('/', function(req, res, next) {
var modeli = mobileRepo.getAllModels();
console.log(modeli);
res.render('index');
});
module.exports = router;
变量 modeli
将变为 undefined
。
通过我的 当前 研究,我知道发生这种情况是因为 connection.query
是一个 async/threaded 函数,但我在任何地方都找不到想弄清楚是如何逃避这个?
基本上,如何将路由和连接结果结合起来,从而允许变量获取查询结果?
我也很乐意接受关于该主题或 Node.js 的任何好的读物,因为我目前正在学习它!
感谢阅读!
编辑:我什至可以看到查询结果在页面加载后出现,但我仍然不知道如何让查询等待。
Tue, 10 Nov 2015 00:42:06 GMT expressnodejs:server Listening on port 3000
undefined <--- this is calling the result from index.js
GET / 200 72.216 ms - 179
[ { model: 'Samsung' }, { model: 'LG' } ] <--- calling result from mobile.js when query is done
我想到的一件事是创建一个结果函数:
module.exports =
{
getAllModels: function( outcome )
{
pool.getConnection( function( err, connection )
{
connection.query("SELECT model FROM product", function( err, res, fie)
{
//snip
outcome( res );
}}}};
但这不会打破为什么 Node.js 如此之快的整个想法吗?
outcome
正是您所需要的,它被称为 callback
,这是使异步代码工作的正确方法。
您还需要更改调用函数的方式。
mobileRepo.getAllModels(function(modeli){
console.log(modeli);
res.render('index');
});
所以有了一个名为 mobile.js
的文件,我创建了一个到数据库的连接,其中有一个调用查询的函数和 returns 一组手机:
var mysql = require('mysql');
var pool = mysql.createPool({
//database information
});
module.exports =
{
getAllModels: function()
{
pool.getConnection( function( err, connection )
{
connection.query("SELECT model FROM product", function( err, res, fie)
{
if( err ) throw err;
connection.release();
//console.log(res);
return res;
});
});
}
};
现在,取消注释上面的代码我确实得到了一个 JSON 对象 [{model: 'LG'}, {model: 'Samsung'}, ...]
,但是当我尝试通过以下方式访问 index.js
路由文件中的那个变量时:
var express = require('express');
var router = express.Router();
var mobileRepo = require('../repositories/mobile');
router.get('/', function(req, res, next) {
var modeli = mobileRepo.getAllModels();
console.log(modeli);
res.render('index');
});
module.exports = router;
变量 modeli
将变为 undefined
。
通过我的 当前 研究,我知道发生这种情况是因为 connection.query
是一个 async/threaded 函数,但我在任何地方都找不到想弄清楚是如何逃避这个?
基本上,如何将路由和连接结果结合起来,从而允许变量获取查询结果?
我也很乐意接受关于该主题或 Node.js 的任何好的读物,因为我目前正在学习它!
感谢阅读!
编辑:我什至可以看到查询结果在页面加载后出现,但我仍然不知道如何让查询等待。
Tue, 10 Nov 2015 00:42:06 GMT expressnodejs:server Listening on port 3000
undefined <--- this is calling the result from index.js
GET / 200 72.216 ms - 179
[ { model: 'Samsung' }, { model: 'LG' } ] <--- calling result from mobile.js when query is done
我想到的一件事是创建一个结果函数:
module.exports =
{
getAllModels: function( outcome )
{
pool.getConnection( function( err, connection )
{
connection.query("SELECT model FROM product", function( err, res, fie)
{
//snip
outcome( res );
}}}};
但这不会打破为什么 Node.js 如此之快的整个想法吗?
outcome
正是您所需要的,它被称为 callback
,这是使异步代码工作的正确方法。
您还需要更改调用函数的方式。
mobileRepo.getAllModels(function(modeli){
console.log(modeli);
res.render('index');
});