-1
我想從MySQL數據庫獲取數據和我使用的Node.js與SQL它,這是我的服務器代碼:的Node.js - SQL函數沒有返回值
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var mysql = require('mysql');
var connection = mysql.createConnection({
host : '127.0.0.1',
user : 'root',
password : '',
database : 'temp'
});
function getData(res){
var tempVal = 1377;
connection.connect();
connection.query('SELECT * FROM tempvalues ORDER BY datetime DESC LIMIT 1', function(err, rows){
console.log(rows);
tempVal = rows;
});
connection.end();
return tempVal;
}
app.get('/', function(req, res){
res.sendfile('index.html');
});
io.on('connection', function(socket){
socket.on('clientSent', function(data){
if(data == "GET")
socket.emit("serverSent", getData());
})
})
http.listen(3000, function(){
console.log('listening on *:3000');
});
如果我去localhost:3000
我只收到1377
作爲值,但不是來自數據庫的實際值,即使控制檯打印了正確的值。這是爲什麼?
謝謝!這工作:) – binaryBigInt