Not being able to save and remove from saved itens using storage


#1

Hello, I’m creating an app that shows posts using a JSON. Right now everything is working but I’m trying to create an saved items list, that’s my html.

<button ion-button clear small color="primary" (click)="saveItem(feed.ID)" icon-left>
    <ion-icon name='bookmark'></ion-icon>
</button>

This is sending a number, 25, 12, 1 for example. to saveItem. My constructor .ts is:

  private itensSalvos: string = '';
  private remove: string = '';

  constructor(public navCtrl: NavController, private alertCtrl: AlertController, public loadingCtrl: LoadingController, public http: Http, public actionSheetCtrl: ActionSheetController, private sharingVar: SocialSharing, public storage: Storage) {
    this.remove = '';
    this.itensSalvos = '';
  }

And my saveItem function is:

  saveItem (post) {
    this.storage.get('saved_posts').then(itens => this.itensSalvos = itens);
    this.doSave (this.itensSalvos, post);
  }

  doSave (itensSalvos, post){
    if (itensSalvos.includes(post)){
      let alert = this.alertCtrl.create({
        title: 'Removed!',
        buttons: ['OK']
      });
      this.remove = post + ",";
      itensSalvos = itensSalvos.replace (this.remove, "");
      alert.present();
      this.storage.set ('saved_posts', itensSalvos);
    } else {
      let alert = this.alertCtrl.create({
        title: 'Saved!',
        buttons: ['OK']
      });
      itensSalvos = itensSalvos + post;
      itensSalvos = itensSalvos + (",");
      alert.present();
      this.storage.set ('saved_posts', itensSalvos);
    }
  }

Every time I click on save I get “saved” 2 times in a row and then “removed” two times in a row and it’s not saving or removing. What am I doing wrong?