第一次访问没有问题,第二次访问会卡在执行SQL的地方。代码如下:
tedious.js
const Connection = require(‘tedious’).Connection;
const Request = require(‘tedious’).Request;
exports.mssql = function(config){
this.connection = new Connection(config);
this.query = function(str,callback){ //执行查询
var connection = this.connection;
var rows={};
connection.on('connect', function(err){ //连接数据库,执行匿名函数
if(err){
callback({'err':err['message']+'请检查账号、密码是否正确,且数据库存在'});
}else{
var request = new Request(str,function(err, rowCount){
if(err)err = {'err':err['message']};
callback(err,rows);
// connection.close();
});
var n=0;
request.on('row', function(columns) { //查询成功数据返回
rows[n]={};
columns.forEach(function(column) {
rows[n][column.metadata.colName] = column.value; //获取数据
});
n++;
});
connection.execSql(request); //执行sql语句
}
});
}
}
client2.js:
const request = require(‘request-json’); var client = request.createClient(‘http://192.168.1.83:8081/’);
var mssql = require(’./tedious.js’); var conn = new mssql.mssql({ ‘userName’: ‘sa’, ‘password’: ‘123’, ‘server’: ‘localhost’, ‘options’: { ‘port’: 1433, ‘database’: ‘WEIGHT20’ } });
var lastId = 0; var timeBw = 1 * 1000;
class postData { _getDate(fc) {
console.log('取称重数据 ID', lastId);
var sql = 'SELECT top 1 * FROM \u79f0\u91cd\u4fe1\u606f where \u5e8f\u53f7>' + lastId + ' order by \u5e8f\u53f7 ';
console.log('SQL', sql);
conn.query(sql, function (err, data) {
if (!err) {
//console.log(data) //成功返回数据
fc(data);
}
else {
fc([]);
console.log(err) //出错返回
}
}
);
}
_postDate(item, fc) {
lastId = item['\u5e8f\u53f7'];
var postData = {};
postData['creator_id'] = item['\u5e8f\u53f7'];
postData['weight_id'] = 'code002';
postData['weight_code'] = 'code002';
postData['weight_rfid'] = '0';
postData['veh_id'] = item['\u6d41\u6c34\u53f7'];
postData['car_no'] = item['\u8f66\u53f7'];
postData['weight_gross'] = item['\u6bdb\u91cd'];
postData['weight_veh'] = item['\u76ae\u91cd'];
postData['weight_net'] = item['\u51c0\u91cd'];
postData['garbage_type'] = item['\u8d27\u540d'];
postData['weight_date'] = item['\u76ae\u91cd\u65f6\u95f4'];
postData['delivery_unit'] = item['\u53d1\u8d27\u5355\u4f4d'];
postData['prj_id'] = 2;
postData['tcar_company_id'] = 0;
postData['creator_id'] = item['\u5e8f\u53f7'];
console.log('post data', postData);
client.post('/jsonapi/cancu_weight/add.json',
postData, function (err, res, body) {
if (err) {
console.error(err);
}
else {
console.log('post resp', body);
fc({ code: 0 });
}
});
}
_time(fc) {
var _self = this;
_self._getDate((d) => {
if (d.length <= 0) {
fc({ code: -1 });
return;
}
_self._postDate(d[0], fc);
})
}
run(fc) {
var _self = this;
_self._time(d => {
setTimeout(function () {
_self.run();
}, timeBw);
}, timeBw)
}
}
var t = new postData(); t.run();
请各位大佬指点