-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