不记录或检索数据

出于某种原因,我无法使用 IndexedDB 方法检索和/或放置任何数据......代码错误还是我遗漏了什么?


我已经放置了几个 console.log 并且我已经添加或尝试添加新值,但据说已经添加了它们(或者至少是成功的消息弹出但控制台日志中没有任何结果显示......


let newDataLine = [{ name: name.value}];


let transaction = db.transaction( ["permit"], "readwrite");


transaction.oncomplete = function() {

statusText.textContent = 'Something was just added';

console.log('-------transaction.oncomplete----------')

console.log(objectStore);


// update the display of data to show the newly added item, by running displayData() again.

displayData();

};


transaction.onerror = function() {

status.textContent = 'Error reading/writing data from/into db error ' + transaction.error;

}


// call an object store that's already been added to the database

let objectStore = transaction.objectStore("permit");


let objectStoreRequest = objectStore.add(newDataLine[0]);


objectStoreRequest.onsuccess = function(event) {

statusText.textContent = 'New permit was just added';

console.log('-------objectStoreRequest.onsuccess----------')

console.log(objectStore);

}

完整代码在https://jsfiddle.net/m7nx9a3v/


这个想法是放置,检索数据并将其填充到表中......提前致谢


MM们
浏览 209回答 1
1回答

holdtom

function displayData() {&nbsp; &nbsp; &nbsp; let objectStore = db.transaction('permit').objectStore('permit');&nbsp; &nbsp; &nbsp; objectStore.openCursor().onsuccess = function(event) {&nbsp; &nbsp; &nbsp; &nbsp; var cursor = event.target.result;&nbsp; &nbsp; &nbsp; &nbsp; if (cursor) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataLine.innerHTML+= '<tr><td>'+cursor.value.name+'</td><td class="buttons"></td></tr>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Name for key ' + cursor.key + ' is ' + cursor.value.name&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cursor.continue();&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log('No more entries!');&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; }参考https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API/Using_IndexedDB
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript