我有一个脚本可以更新几乎正常工作的数据库。由于我控制之外的一些项目,我无法使用Put进行更新/插入。我必须使用的过程有点像这样。
addRecords
函数,获取对象的每一行并获取Get on 3数据元素,如果有匹配则将记录ID与其余数据一起传递给Put ajax函数,如果有的话没有匹配将数据传递给Post ajax函数。
目前,如果数据文件足够大,那么ajax Get函数确实会给出500(内部服务器错误)。这似乎只发生在< 1记录/ 1000的速度。
我认为我只是在重载服务器。我想找到一些方法来“减少”ajax调用,以便服务器不会过载。
我试图编辑一个if语句,看看有多少已发送,有多少已完成,但没有取得任何成功。限制addRecords函数当前数据量的最佳方法是什么?
添加记录功能:
function addRecords(lines) {
lines.forEach(function(v) {
$j.ajax(
"/ws/schema/table/U_STATEADM",
{
"type": "GET",
"data": 'q=student_key==' + v.STUDENT_KEY + ';schoolid==' +
v.SCHOOLID + ';report_period==' + v.REPORT_PERIOD +
'&projection=id',
"indexValue": v,
"datatype": "text",
"headers": {
"Content-Type": "application/json"
},
"error": function(data) {
// alert("There was an error!");
},
"success": function(data) {
if (data.record.length == "1") {
//update record
//gives record id
var newid = data.record[0].id
//gives reply from GET statement
//gives information for what was submitted on GET
//works, takes id from GET and information sent to get and updates database
{
$j.ajax(
"/ws/schema/table/U_STATEADM/" + newid,
{
"type": "PUT",
"data": JSON.stringify({
"id": newid,
"name": "U_STATEADM",
"tables": {
"U_STATEADM": v
}
}),
"headers": {
"Content-Type": "application/json"
}
}
);
};
} else if (data.record.length == "0") {
alert("No Record Found");
//insert record using post
//works to insert new lines
{
$j.ajax(
"/ws/schema/table/U_STATEADM",
{
"type": "POST",
data: JSON.stringify({
"tables": {
"u_stateadm": v
}
}),
dataType: "json",
"headers": {
"Content-Type": "application/json"
}
}
);
}
}
},
"complete": function() {
completed++;
// console.log(completed, "End Completed");
}
}
);
}
创建对象并将其发送到addRecords函数的函数的一部分:
for (var i = 4; i < allTextLines.length; i++) {
var data = allTextLines[i].split(',');
//Add key to array
var newkey = data[0] + data[4] + data[5];
// data.splice(0,0,newkey);
//remove unused data elements
data.splice(1, 3);
data.splice(3, 1);
if (data.length == headers.length) {
lt++;
var tarr = {};
for (var j = 0; j < headers.length; j++) {
tarr[headers[j]] = data[j];
}
lines.push(tarr);
}
}
//try to get back id from data
//loop through all lines in file
//This version does give objects of success linked from information sent on
// get to information returned on get, from
addRecords(lines);