Error in fetching data from Storage


#1

hello everyone,
I am trying to fetch all the values which I have stored in my local-storage,and the output should be like this:

But I am getting the output like this:

I have created the form in which I am entering the values and want to fetch all the names of the account here,but clicking on the account I am getting the result like this.

this is my AddaccountProvider.ts code:

export class AddaccountProvider {

  constructor(public http: Http,private storage:Storage) {   //
 
console.log('Hello AddaccountProvider Provider'); 
 }
  set(key, account)
{      
  this.storage.set(key, account);  
  }

    get(key)
{  

      return this.storage.get(key);    
}



isAccountPresent()
{      
  return new Promise((resolve, reject) => {    
        this.storage.forEach( (value, key, index) => {    
                     
    resolve(true)  
          });      
  });         
   }
getAccountList(){   
     return new Promise((resolve, reject) => {        
    let accounts = [];           
 this.storage.forEach( (value, key, index) => {          
      // value = JSON.parse(value);  
  
            accounts.push(key);       
     });            setTimeout(() =>{        
        if(accounts && accounts.length > 0){    
                // console.log(accounts);             
       resolve(accounts);        
        }            
   },
 1000);       
 });   
 }
getAllAccounts(){     
   return new Promise((resolve, reject) => {  
          let accounts = [];      

      this.storage.forEach( (value, key, index) => {      
          value = JSON.parse(value);               
 accounts.push({name: key, created: new Date(value.created).toLocaleDateString() ,
 budget: value.budget, obalance: value.balance });        

    });      
      setTimeout(() =>{           
     if(accounts && accounts.length > 0){      
              // console.log(accounts);                
    resolve(accounts);                }              
 }, 
1000);    
    });

}

this is the function of loading the account:

logEntry(){      

 
                     if(!this.accountSelected) 

{
alert(“Please select an account first”);
return;
}

this.store.get(this.accountSelected).then(data =>
{ this.register = JSON.parse(data) || [];

    if(!this.entry.deposit || this.entry.deposit == '') this.entry.deposit = 0;      

if(!this.entry.withdraw || this.entry.withdraw == ‘’) this.entry.withdraw = 0;

    let balance  = this.entry.deposit - this.entry.withdraw;         

console.log("Add “+this.register.checkbook[this.register.checkbook.length-1].balance+” & "+balance);
balance = parseInt(balance.toString()) + parseInt(this.register.checkbook[this.register.checkbook.length-1].balance);

        let entryData = { date: this.entry.date, num: this.entry.num, description: this.entry.description,  
                    r: this.entry.r, withdraw: 

this.entry.withdraw,
deposit: this.entry.deposit, balance: balance
};
this.register.checkbook.push(entryData);

        console.log(JSON.stringify(this.register));

         this.store.set(this.accountSelected, JSON.stringify(this.register));
        });        

    
}    loadCheckbook(account)

{ this.store.get(account).then(data =>
{ this.register = JSON.parse(data) ||
[]; console.log(“Updating…”);
});
}

updateSelectedAccount(account)

{
this.accountSelected = account;
this.loadCheckbook(account);

}
Thanks in advance


#2

Just sorry for the above code:

now its formatted properly:


logEntry(){      

 
                     if(!this.accountSelected)

{      
    alert("Please select an account first"); 
         return;     
 }

this.store.get(this.accountSelected).then(data =>
 {            this.register = JSON.parse(data) || [];

    
    if(!this.entry.deposit || this.entry.deposit == '') this.entry.deposit = 0;

  if(!this.entry.withdraw || this.entry.withdraw == '') this.entry.withdraw = 0;

    let balance  = this.entry.deposit - this.entry.withdraw;

   console.log("Add "+this.register.checkbook[this.register.checkbook.length-1].balance+" & "+balance);      
    balance = parseInt(balance.toString()) + parseInt(this.register.checkbook[this.register.checkbook.length-1].balance);

        let entryData = { date: this.entry.date, num: this.entry.num, description: this.entry.description,  
                    r: this.entry.r, withdraw:

this.entry.withdraw,                  
     deposit: this.entry.deposit, balance: balance   
       };
         this.register.checkbook.push(entryData);

        console.log(JSON.stringify(this.register));

         this.store.set(this.accountSelected, JSON.stringify(this.register));
        });        

    
}    loadCheckbook(account)

{        this.store.get(account).then(data =>
 {            this.register = JSON.parse(data) || 
[];            console.log("Updating.."); 
     }); 
   }

updateSelectedAccount(account)

{      
this.accountSelected = account;      
this.loadCheckbook(account); 

   }

#3

First I would like to clean up the provider.

  • define business interfaces and type everything
export interface Account {
  name: string;
  created: string;
  budget: number;
  obalance: number;
  // whatever else goes in here
}
  • never use setTimeout()
  • never instantiate needless Promises
  • let Storage handle JSON marshalling/unmarshalling
  • getAccountList() and getAllAccounts() seem to do the same thing
  • don’t expose direct Storage methods out of the provider API
export class AccountProvider {
  constructor(private _storage: Storage) {}
  isAccountPresent(): Promise<boolean> {
    return this._storage.keys().then((keys) => keys.length > 0);
  }
  accountByName(name: string): Promise<Account> {
    return this._storage.get(name);
  }
  updateAccount(acct: Account): Promise<void> {
    return this._storage.set(acct.name, acct);
  }
  allAccounts(): Promise<Account[]> {
    let rv = [] as Account[];
    return this._storage.forEach((acct) => rv.push(acct))
      .then(() => rv);
  }
}

#4

Thanks a lot.the error has been resolved.