【问题标题】:Is there a solution to the error "toPromise is not a function" in Angular?Angular 中的“toPromise 不是函数”错误是否有解决方案?
【发布时间】:2022-01-23 05:48:24
【问题描述】:

我在测试时不断收到此错误:未捕获错误:未捕获(承诺中):TypeError:this._productService.getProducts(...).toPromise is not a function。

我知道 toPromise 在 rxjs 版本 7 中已被弃用,但我正在运行版本 6。有没有办法解决这个问题或以其他方式在不使用 toPromise 的情况下编写我的代码?

我根据之前点击定义的三个输入来获取我的产品。

谢谢。

    async ngOnInit(): Promise<void> {
    this.product = await this._productService
      .getProducts({
        InputOne: this.inputOne,
        InputTwo: this.inputTwo,
        InputThree: this.inputThree
      })
      .toPromise();
  }

【问题讨论】:

    标签: angular unit-testing service promise rxjs


    【解决方案1】:

    由于 RxJS 7.0 toPromise() 已被弃用,取而代之的是 firstValueFromlastValueFrom,具体取决于您想要做什么。

    【讨论】:

    • 嗨,我仍在运行第 6 版,而 toPromise() 仍然无法正常工作。 firstValueFrom 或 lastValueFrom 在 6 中还不起作用
    • 好吧,那就看看getProducts()返回什么
    猜你喜欢
    • 2021-11-12
    • 1970-01-01
    • 2021-06-26
    • 1970-01-01
    • 2020-12-16
    • 1970-01-01
    • 1970-01-01
    • 2019-09-21
    • 1970-01-01
    相关资源
    最近更新 更多