使用Dexie Js获取保存在 IndexedDB 中的记录数

xsuvu9jc  于 12个月前  发布在  IndexedDB
关注(0)|答案(1)|浏览(278)

我是Angular和JS的新手。现在我写的代码保存数据到 IndexedDB ,我想显示的记录数是挂起( IndexedDB )导航栏,我无法得到的记录总数保存到 IndexedDB 。
我使用下面的代码来获取计数,但它只能在箭头函数/回调函数中使用。我尝试将它赋值给全局变量,但它返回null。
下面是代码,我想利用这些方法并返回IDB中存在的记录数。

private countIDBRecords() {
    this.db.myTableName.toCollection().count( (count: string) => {
      
      console.log(count + " records in total");  // this returns correct value, but i cannot use 'count' outside this function
  
      this.totalRecords = count;
  });  

  console.log(this.totalRecords); //return null

}
13z8s7eq

13z8s7eq1#

所以count返回了promise函数沿着我需要的值。首先,我找到了这个答案:Using Getter and Setter to assign value to a local variable
后来,我发现使用一个简单的blog和await就可以解决我的问题,

async countIDBRecords() {
    var data = await this.db.tableName.count();
    console.log('Total records in tableName: ',data);
  }

相关问题