Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript react.js indexDB大型数据存储.put性能问题_Javascript_Reactjs_Storing Data - Fatal编程技术网

Javascript react.js indexDB大型数据存储.put性能问题

Javascript react.js indexDB大型数据存储.put性能问题,javascript,reactjs,storing-data,Javascript,Reactjs,Storing Data,我有一个可以有数百万行的对象数组。我正在尝试将它存储在indexDB中,我可以成功地完成这项工作,但它在“put”方法上存在一些性能问题。原因是我必须循环遍历每个索引,并将其分别放入indexDB中。有没有办法在这里简单地转储整个阵列?这是我的密码 var indexDB; indexDB = window.indexedDB; var open = indexedDB.open("HistoricalDB"); open.onupgrade

我有一个可以有数百万行的对象数组。我正在尝试将它存储在indexDB中,我可以成功地完成这项工作,但它在“put”方法上存在一些性能问题。原因是我必须循环遍历每个索引,并将其分别放入indexDB中。有没有办法在这里简单地转储整个阵列?这是我的密码

    var indexDB;
    indexDB = window.indexedDB; 
    var open = indexedDB.open("HistoricalDB");
    open.onupgradeneeded = function(){
    let db = open.result;
    let store = db.createObjectStore("HistoricalTable", {keyPath: "id"});
    let index = store.createIndex("CIndex", ["time", "value"]);
    };
    open.onsuccess = function(){
        let db = open.result;
        let tx = db.transaction("HistoricalTable", "readwrite");
        let store = tx.objectStore("HistoricalTable");
        let index = store.index("CIndex");
        for (let i=0; i<allHistoricalData.length-1; i++){
            store.put(allHistoricalData[i]);
        }
        let getAll = store.getAll();
        getAll.onsuccess = function(){
            console.log(getAll.result)
        }
        tx.oncomplete = function(){
            db.close();
        };
    }
var indexDB;
indexDB=window.indexedDB;
var open=indexedDB.open(“HistoricalDB”);
open.onupgradeneeded=函数(){
设db=open.result;
let store=db.createObjectStore(“HistoricalTable”,{keyPath:“id”});
让index=store.createIndex(“CIndex”,“time”,“value”);
};
open.onsuccess=函数(){
设db=open.result;
设tx=db.transaction(“HistoricalTable”、“readwrite”);
let store=tx.objectStore(“历史表”);
let index=store.index(“CIndex”);

for(让i=0;iIndexDB可以存储任何类型的javascript可克隆数据,因此您应该能够简单地将数组作为一个整体存储

我看到您正在创建一个索引并使用它来存储值。IndexDB索引用于查找存储中的数据,但不应用于存储数据本身

您要做的是将数据直接放入存储区,如下所示:

var open=window.indexedDB.open('HistoricalDB');
open.onupgradeneeded=函数(){
设db=open.result;
db.createObjectStore('HistoricalTable');
};
open.onsuccess=函数(){
设db=open.result;
设tx=db.transaction('HistoricalTable','readwrite');
let store=tx.objectStore('HistoricalTable');
let request=store.put(allHistoricalData,'DATA');
request.onsuccess=函数(){
console.log('success!');
};
request.onerror=函数(){
console.log(request.error);
};
};

我应该这样创建存储吗……让store=db.createObjectStore(“HistoricalTable”);是的,如果不使用键路径,则不必指定任何选项。请注意,如果要存储数百万行,则put函数可能需要一段时间。这样做的目的是让该过程异步进行,以便应用程序可以继续运行。能否显示完整的代码?我仍然有问题。我的编译崩溃在我身上。更新的答案显示完整的代码来存储数据,我会让你处理数据检索
        for (let i=0; i<allHistoricalData.length-1; i++){
            store.put(allHistoricalData[i]);
        }