Fail to return a string from in a function from another function

53 Views Asked by At

I have this 3 functions that triggers POST api calls, and I want to return a succsess/failure message to whoever calls it (Observable<string>).

So the messages are similar and I didnt wan to repeat myself then I added a new function to get success message. But from some reason it dosent work, I see a blank page when I run the app, but if instead of calling getSuccessMessage I just pass a regular string ("Something") it works...

weird, those are my funcs:

export enum ListType {Cars, Animals, Books}

 public updateOne(carList: Car[]): Observable<string> {
    return this._myApiSevice.postCarsNewOrder(carList)
      .map(response => this.getSuccessMessage(ListType.Cars)) //update succeeded  
      .catch(error => Observable.of("FAILURE: post to Cars did not succeed!")); 
  }

  public updateTwo(animalList: Animal[]): Observable<string> {
    return this._myApiSevice.postAnimalsNewOrder(animalList)
      .map(response => this.getSuccessMessage(ListType.Animals))
      .catch(error => Observable.of("FAILURE: post to Animals did not succeed!"));
  }

  public updateThree(bookList: Book[]): Observable<string> {
    return this._myApiSevice.postBooksNewOrder(bookList)
      .map(response => this.getSuccessMessage(ListType.Books)) //update succeeded
      .catch(error => Observable.of("FAILURE: post to Books did not succeed!"));
  }

  public getSuccessMessage(listType: ListType): string {
    return "SUCCESS: post to " + ListType[listType] + "succeed!";
  }

do you see something wrong?

this is the error in the console:

EXCEPTION: Error: Uncaught (in promise): Cannot resolve all parameters for 'MyCmp'(undefined, ElementRef, MdDialog). Make sure that all the parameters are decorated with Inject or have valid type annotations and that 'MyCmp' is decorated with Injectable.

the enum is declerated in another class, this importing the class the the enums are coming from.

and also if you have a suggestion for me how to gather also the failure message into a one function with the success message it will be lovely, thanks@!

1

There are 1 best solutions below

0
Marshall Hampson On

To get the value of a string from an enum type in typescript you need to do the following

enum ListType {
  Cars, 
  Animals, 
  Books
}
var booksStr = ListType[ListType.Books];

Here's a good explanation for why this is the case https://basarat.gitbooks.io/typescript/content/docs/enums.html

So your function would be something like

public getSuccessMessage(listType: ListType): string {
    return "SUCCESS: post to " + ListType[ListType.listType] + "succeed!";
}