使用 Dexie Js 获取索引数据库中保存的记录数

问题描述 投票:0回答:1

我是 Angular 和 JS 的新手。现在我正在编写代码将数据保存到索引数据库,我想显示“否”。导航栏上待处理的记录(在索引数据库中),我无法获取保存到索引数据库的记录总数。

我使用下面的代码来获取计数,但它只能在箭头函数/回调函数内使用,我尝试将其分配给全局变量,但它返回 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

}
angular indexeddb dexie
1个回答
0
投票

因此计数返回了 Promise 函数以及我需要的值。所以,首先我找到了这个答案:使用 Getter 和 Setter 为局部变量赋值

后来,我发现使用简单的 async 和 wait 就可以解决我的问题,

async countIDBRecords() {
    var data = await this.db.tableName.count();
    console.log('Total records in tableName: ',data);
  }
© www.soinside.com 2019 - 2024. All rights reserved.