无法从另一个函数返回函数中的字符串

时间:2022-02-03 16:04:01

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>).

我有这3个函数触发POST api调用,我想向任何调用它的人发送succsess / failure消息(Observable )。

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...

所以消息是相似的,我不想重复自己,然后我添加了一个新功能来获得成功消息。但是由于某种原因它起作用,我在运行应用程序时看到一个空白页面,但如果不是调用getSuccessMessage,我只是传递一个常规字符串(“Something”)它的工作原理......

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.

EXCEPTION:错误:未捕获(在承诺中):无法解析'MyCmp'的所有参数(undefined,ElementRef,MdDialog)。确保所有参数都使用Inject进行修饰或具有有效的类型注释,并且“MyCmp”使用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 个解决方案

#1


0  

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

要从typescript中的枚举类型获取字符串的值,您需要执行以下操作

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

以下是为什么会出现这种情况的一个很好的解释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!";
}

#1


0  

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

要从typescript中的枚举类型获取字符串的值,您需要执行以下操作

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

以下是为什么会出现这种情况的一个很好的解释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!";
}