【发布时间】:2020-11-28 14:36:27
【问题描述】:
我正在尝试将一个大型 JSON 发布到我的数据库中,该 JSON 至少有 400.000 个对象。
如果我剪切文件并尝试发布 20.000 个对象,一切正常,所以问题应该是 JSON 的大小。
我已将 JSON 分成 20 个块,我的想法是一次上传一个,但我正在努力让它工作。
这是我正在使用的:
var rows = {};
Papa.parse(content, {
header: false,
delimiter: '|',
worker: true,
encoding: "utf16le",
dynamicTyping: true,
skipEmptyLines: true,
complete: function(results) {
rows = results.data;
let obj = []
for(var i=0; i < rows.length; i++){
obj.push(rows[i])
}
let result = []
for(let i in obj) {
let temp = {}
if(i > 0) {
temp["id"] = obj[i][0]
temp["name"] = obj[i][1]
temp["tel"] = obj[i][2]
temp["email"] = obj[i][3]
temp["status"] = obj[i][5]
result.push(temp)
}
}
var array1 = result.map((e) => {
return {
id: e.id,
name: e.name,
email: e.email
}
})
let chunked = []
let size = 20000;
Array.from({length: Math.ceil(array1.length / size)}, (val, i) => {
chunked.push(array1.slice(i * size, i * size + size))
})
console.log(chunked); // at this point I have my array divided into chunks of 20000
axios({
url: 'url',
method: 'post',
data: chunked
})
.then(function (response) {
// your action after success
console.log(response);
})
.catch(function (error) {
// your action on error successif (error.response) {
console.log(error);
});
【问题讨论】:
-
您已经分成了 20 个块,即“分块”,但仍然发送整个数组,这与发送相同数量的数据相同,因为没有分割它。更好地循环遍历您的“分块”发送。
-
你好,谢谢你的回答,我试过循环,我只是不知道如何一个一个发送,不管我做什么我想我总是一次发送整个数组
标签: javascript arrays axios chunks papaparse