I am new to Angular and JS. Right now I am writing code to save data to Indexed db and I want to show number of records which are pending (in indexed db) on navigation bar, I am not able to get the total number of records saved to Indexed DB.
I am using the codes below to get the count, but it is only usable inside the arrow function/callback function. I have tried assigning it to global variable, but it returns null.
Here is the code, I want to utilize these methods and return the number of records which exists in the 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
}
So the count was returning promise function along with the value which I needed. So, first I found this answer : Using Getter and Setter to assign value to a local variable
Later, I found out that using a simple async and await would resolve my problem,
async countIDBRecords() {
var data = await this.db.tableName.count();
console.log('Total records in tableName: ',data);
}