问题描述
node接受请求查询数据库,查询带有值,返回时候出错 报错write after end
相关代码
// console.log(startTime,id,endTime)
var express = require('express');
var app = express();
var mysql = require('mysql');
var connection = mysql.createConnection({
host: '127.0.0.1', // 接口
user: 'root', // 用户名
port : '3306',
password: '', //密码
database: "lbst" // 表
});
connection.connect();
if(startTime != null) {
// var sql='select from lb_admin_addon where starTime= '+startTime+' and id='+ id +' and endTime='+endTime;
// select * from market_orders where createdate>='2010-03-01 00:00:00' and createdate<'2016-03-02 00:00:00'
//
var sql = `
SELECT * FROM lb_addmin_taglist WHERE timestamp>='${startTime}' AND timestamp<'${endTime}'
`
} else {
var sql = 'select * from lb_admin_msg';
}
var sqlarr = [];
connection.query(sql, function selectCB(err, results, fields) {
if(err) {
throw err;
}
if(results) {
for(var a = 0; a < results.length; a++) {
sqlarr.push(results[a])
}
}
connection.end();
})
function ztstInterval() {
clearInterval(searchArr)
}
var num = 0;
var searchArr = setInterval(function() {
num++;
console.log(sqlarr.length)
if(sqlarr.length != 0) {
ztstInterval();
sqlarr = JSON.stringify(sqlarr)
// response.writeHead(200, {'Content-Type': 'text/plain; charset=utf-8'});
var msg = {
"数据": 200,
data: sqlarr
}
response.write(1);
response.end(msg);
}
}, 1000)