Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/450.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 使用Dexie的本地存储不保持持久性_Javascript_Local Storage_Persistent Storage_Dexie - Fatal编程技术网

Javascript 使用Dexie的本地存储不保持持久性

Javascript 使用Dexie的本地存储不保持持久性,javascript,local-storage,persistent-storage,dexie,Javascript,Local Storage,Persistent Storage,Dexie,我正在使用Dexie访问闪存卡制作项目中的IndexedDB。我可以按预期操作数据库,如果我关闭并重新打开浏览器,它将保留在数据库中,但通常当我在几天没有处理项目后打开项目时,我放入数据库中的存储数据不再存在 我做了一些研究,发现我需要使数据库持久化,但我不知道如何做到这一点 我已经将推荐的代码从Dexie复制到主页index.js的onLoad函数中。以下是相关代码: //When the page loads, display the decks in ul on the webpage.

我正在使用Dexie访问闪存卡制作项目中的IndexedDB。我可以按预期操作数据库,如果我关闭并重新打开浏览器,它将保留在数据库中,但通常当我在几天没有处理项目后打开项目时,我放入数据库中的存储数据不再存在

我做了一些研究,发现我需要使数据库持久化,但我不知道如何做到这一点

我已经将推荐的代码从Dexie复制到主页index.js的onLoad函数中。以下是相关代码:

//When the page loads, display the decks in ul on the webpage.
window.addEventListener('load', onLoad);
async function onLoad() {
   
   //Check how much data is available.
   showEstimatedQuota();
 
   //Make sure the storage is persistent.
   isStoragePersisted().then(async isPersisted => {
     if (isPersisted) {
       console.log(":) Storage is successfully persisted.");
     } else {
       console.log(":( Storage is not persisted.");
       console.log("Trying to persist..:");
       if (await persist()) {
         console.log(":) We successfully turned the storage to be persisted.");
       } else {
         console.log(":( Failed to make storage persisted");
       }
     }
   });
}
上面的onLoad函数引用了我保存在dexie-setup.js上的三个函数:

//This function makes the storage persistent.
//(Copied from https://dexie.org/docs/StorageManager)
async function persist() {
  return await navigator.storage && navigator.storage.persist &&
    navigator.storage.persist();
}


//This function checks if the storage is persistent.
//(Copied from https://dexie.org/docs/StorageManager)
 async function isStoragePersisted() {
  return await navigator.storage && navigator.storage.persisted &&
    navigator.storage.persisted();
}


//This function logs to console how much data is available.
//(Copied from https://dexie.org/docs/StorageManager)
async function showEstimatedQuota() {
  if (navigator.storage && navigator.storage.estimate) {
    const estimation = await navigator.storage.estimate();
    console.log(`Quota: ${estimation.quota}`);
    console.log(`Usage: ${estimation.usage}`);
  } else {
    console.error("StorageManager not found");
  }
}
我的控制台日志:

  • dexie setup.js:56配额:6358499328

  • dexie setup.js:57用法:25370

  • index.js:30:(存储未持久化

  • index.js:31正在尝试 坚持

  • js:84检查完dexie

  • index.js:33:)我们成功地将存储持久化

但是,如果我刷新页面,我会在控制台上记录相同的内容:数据库仍然设置为not persistent

showEstimatedQuota函数检查数据存储并确认DataStorage API正在运行,因此我认为这不是问题所在。(我只存储包含文本的小对象,因此无论如何,我不希望超出存储限制。)

到目前为止,这个项目在我的chromebook上完全是本地的,我正在Chrome浏览器上查看它

请让我知道如何使我的数据库持久化。我对这一点很陌生(这是我关于stackoverflow的第一个问题!),所以希望这是一个容易解决的问题!提前谢谢