Hi there,

I have not been able to resolve the error "Property 'then' does not exist 
on type 'void'".

I here by attach the two files. The file, app.component.ts, is the one 
which contains the problematic  snippet
below....

  private removeAcc(index:number){
   this._accountService.remove(index).then(account => console.log(account));
}

Any help to get it resolved  highly appreciated.

Thanks & Regards,
John.

 


On Saturday, 10 June 2017 17:34:36 UTC+2, Sander Elias wrote:
>
> Indeed, it does not return a promise. You can't put a .then on a void the 
> error is spot on.
>
> Regards
> Sander
>

-- 
You received this message because you are subscribed to the Google Groups 
"Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.
import {Injectable,Optional} from '@angular/core';
import {Account} from "./account.model";
import {LoggerService} from './util/logger.service';

@Injectable()
export class AccountService{
constructor(@Optional() private _logger:LoggerService){} //1

  private _accounts:Array<Account> = [{
    id:1,
    title: "Global Bank",
    description: "Main bank account.",
    balance: 567
  },
  {
    id:2,
    title:"Pacific Bank",
    description: null,
    balance: 322
    }];

    public getAll():Promise<Array<Account>>{
      return Promise.resolve(this._accounts);
    }

    private _nextId = 3;
    private _accountLimit = 3;
    public create(newAccount:Account){

      return new Promise((resolve,reject) => {
        if(this._accounts.length >= this._accountLimit)
          return reject("Maximum accounts limit reached.");

      newAccount.id = this._nextId++;
      if(this._logger)
        this._logger.log('Account created: ' + newAccount.title);
      this._accounts.push(newAccount);
      resolve(newAccount);
   });
}

  public remove(index:number){
  if(this._logger)
    this._logger.log('Account deleted: ' + this._accounts[index].title);

  this._accounts.splice(index, 1);
  }
}

export let ACCOUNT_SERVICE_PROVIDERS:Array<any> = [AccountService, LoggerService];
import {Component, ViewChild, Injector} from '@angular/core';
import {Account} from './account/account.model';
import {AccountsList} from './account/accounts_list.component';
import {AccountForm} from './account/account_form.component';
import {AccountService, ACCOUNT_SERVICE_PROVIDERS} from './account/account.services'; //

@Component({
  selector: 'my-app',
  templateUrl: 'app/app.component.html',
  styleUrls:['app/app.component.css'],
  directives:[AccountsList, AccountForm],
  providers:[ACCOUNT_SERVICE_PROVIDERS]
})
export class AppComponent {

  private _accounts:Array<Account>;

  private _accountService:AccountService;
  constructor(accountService: AccountService){

    this._accountService = accountService;
    var promise = this._accountService.getAll(); //
    promise.then(accounts=> this._accounts = accounts); //
  }

  private createAccError:string = ""

  private createAcc(newAccount:Account){
    this._accountService.create(newAccount).then(account => {
        console.log(account);
        this.createAccError = "";
        this.form.resetForm();
   }).catch(err => this.createAccError = err);
}

  private removeAcc(index:number){
   this._accountService.remove(index).then(account => console.log(account));
}


  @ViewChild(AccountForm) form: AccountForm;
}

Reply via email to