Javascript 错误:将数据插入表时读取ECONRESET
我按照本教程()将多行数据插入到MySQL表中(“一次插入多行”一章) 我对我的代码做了同样的操作,但是如果数据库足够好的话,我得到了一个EconReset错误,而不是我的连接 错误详细信息如下:Javascript 错误:将数据插入表时读取ECONRESET,javascript,mysql,node.js,Javascript,Mysql,Node.js,我按照本教程()将多行数据插入到MySQL表中(“一次插入多行”一章) 我对我的代码做了同样的操作,但是如果数据库足够好的话,我得到了一个EconReset错误,而不是我的连接 错误详细信息如下: { Error: read ECONNRESET at _errnoException (util.js:1022:11) at TCP.onread (net.js:628:25) -------------------- at Protocol._enqueue (
{ Error: read ECONNRESET
at _errnoException (util.js:1022:11)
at TCP.onread (net.js:628:25)
--------------------
at Protocol._enqueue (C:\Users\frederic.fayet\Desktop\DEV\NodeJS\TER_V1 - Reprise après pause\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Connection.query (C:\Users\frederic.fayet\Desktop\DEV\NodeJS\TER_V1 - Reprise après pause\node_modules\mysql\lib\Connection.js:200:25)
at C:\Users\frederic.fayet\Desktop\DEV\NodeJS\TER_V1 - Reprise après pause\app.js:340:24
at handlePromises (C:\Users\frederic.fayet\Desktop\DEV\NodeJS\TER_V1 - Reprise après pause\app.js:242:9)
at promise.then (C:\Users\frederic.fayet\Desktop\DEV\NodeJS\TER_V1 - Reprise après pause\app.js:251:20)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
code: 'ECONNRESET',
errno: 'ECONNRESET',
syscall: 'read',
fatal: true }
数据库连接为:
db.connect(function(err) {
if (err) console.error('Error occured during the DB connection.');
console.log("DB connection: OK");
});
sqlDataSchedules是一个如下所示的数组:
[ [ '848633',
'Amiens',
'162700',
'162700',
'20190509',
'20190702',
2019-05-31T07:27:09.710Z ],
[ '848633',
'Villers-Bretonneux',
'163800',
'163900',
'20190509',
'20190702',
2019-05-31T07:27:09.710Z ],
[ '849401',
'St Quentin',
'074500',
'074500',
'20190527',
'20190528',
2019-05-31T07:27:09.711Z ],
... 141377 more items ]
SQL请求是:
INSERT INTO schedules (trainNumber, stopName, baseArrivalTime, baseDepartureTime, beginDate, endDate, updatedOn) VALUES ?
我终于找到了一个使用for循环和异步函数的解决方案:
async function loopOverRequests(data, sqlRequest, tableName) {
let nbRequests = 0;
for (var i = 0; i<data.length; i++) {
nbRequests = nbRequests + await makeTheRequest(data[i], sqlRequest);
}
console.log(nbRequests + ' imported in the table `'+ tableName + '`.');
return new Promise((resolve, reject) => {
resolve(nbRequests);
});
}
function makeTheRequest(el, sqlRequest) {
return new Promise((resolve, reject) => {
db.query(sqlRequest, el, function (err, result) {
if (err) {
console.log(err);
reject(err); //throw err;
} else {
resolve(result.affectedRows);
}
});
});
}
异步函数loopOverRequests(数据、sqlRequest、tableName){
设nbRequests=0;
对于(var i=0;i{
解决(请求);
});
}
函数MaketRequest(el、sqlRequest){
返回新承诺((解决、拒绝)=>{
查询(sqlRequest、el、函数(err、result){
如果(错误){
控制台日志(err);
拒绝(错误);//抛出错误;
}否则{
解决(结果受影响的行为);
}
});
});
}
只需更改MySQL允许的最大数据包即可。例如:
max_allowed_packet=100M
在my.ini或my.cnf中。(别忘了重新启动MySQL实例服务)
它解决了我的问题
max_allowed_packet=100M