我正在使用的MySQL的NodeJS包來查詢我的結果,但在檢索數據MySQL查詢返回空對象
具有麻煩var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'PASSWORD',
database : 'ambuCenter'
});
var data;
function do_queries(callback){
connection.connect();
connection.query(
"SELECT * FROM customers",
function(err,result,fields){
if (err) return callback(err,null);
callback(err,result);
}
);
connection.end();
}
function request_handler(){
do_queries(
function(err, result){
if(err) throw err;
data = JSON.stringify(result);
data = JSON.parse(data);
console.log(data);
}
);
}
request_handler();
console.log(data);
第一的console.log(數據)被返回的對象, 但一個在端返回的值未定義
Manishs-MacBook-Air:Models manishbaghel$ node db_model.js
undefined
[ { id: 1, name: 'John', address: 'Highway 71' },
{ id: 2, name: 'Peter', address: 'Lowstreet 4' },
{ id: 3, name: 'Amy', address: 'Apple st 652' },
{ id: 4, name: 'Hannah', address: 'Mountain 21' },
{ id: 5, name: 'Michael', address: 'Valley 345' },
{ id: 6, name: 'Sandy', address: 'Ocean blvd 2' },
{ id: 7, name: 'Betty', address: 'Green Grass 1' },
{ id: 8, name: 'Richard', address: 'Sky st 331' },
{ id: 9, name: 'Susan', address: 'One way 98' },
]
當您定義'data'時,會導致它被設置爲鍵入'undefined'。其餘部分是nodejs的'async'性質 –
那麼我該如何檢索數據 –