Type 'Promise<any>' is not assignable to type 'string'

huangapple go评论103阅读模式
英文:

Type 'Promise<any>' is not assignable to type 'string'

问题

我正在使用 async 和 promise 如下所示,我想等待 img 值返回。

  1. ngOnInit(): void {
  2. ...
  3. ...
  4. this.auth.msg.subscribe((res) => {
  5. let img = this.fetchImage(this.user.id);
  6. this.url = img; // 获取错误
  7. ...
  8. });
  9. }
  10. async fetchImage(Id) {
  11. let result: any;
  12. let payload = {
  13. Id: Id
  14. };
  15. result = await this.Sev.getPicture(payload).toPromise();
  16. return result.response;
  17. }

当我将 img 响应传递给其他变量时出现此错误:

类型 'Promise' 不能赋值给类型 'string'

英文:

I'm using async and promise like given below, I want to wait for the img value to be returned.

  1. ngOnInit(): void {
  2. ...
  3. ...
  4. this.auth.msg.subscribe((res) =&gt; {
  5. let img=this.fetchImage(this.user.id);
  6. this.url=img; // getting error
  7. ...
  8. });
  9. }
  10. async fetchImage(Id) {
  11. let result: any;
  12. let payload = {
  13. Id: Id
  14. };
  15. result = await this.Sev.getPicture(payload).toPromise();
  16. return result.response;
  17. }

When I pass img response to some other variable getting this error:

> Type 'Promise<any>' is not assignable to type 'string'

答案1

得分: 2

The error message mentions that your fetchImage returns a Promise.

To get the value from the Promise, either:

  1. 使用回调函数
  1. this.auth.msg.subscribe((res: any) => {
  2. this.fetchImage(this.user.id)
  3. .then((value) => {
  4. this.url = img;
  5. });
  6. });
  1. 使用 async/await
  1. this.auth.msg.subscribe(async (res: any) => {
  2. let img = await this.fetchImage(this.user.id);
  3. this.url = img;
  4. });

.toPromise() 已经在 RxJS 7 中弃用。建议根据RxJS文档中所述,将 .toPromise() 迁移到 firstValueFrom/lastValueFrom

  1. import { lastValueFrom } from 'rxjs';
  2. async fetchImage(Id)
  3. {
  4. let result: any;
  5. let payload = {
  6. Id: Id
  7. };
  8. result = await lastValueFrom(this.Sev.getPicture(payload));
  9. return result.response;
  10. }
英文:

The error message mentions that your fetchImage returns a Promise.

To get the value from the Promise, either:

  1. A callback function
  1. this.auth.msg.subscribe((res: any) =&gt; {
  2. this.fetchImage(this.user.id)
  3. .then((value) =&gt; {
  4. this.url = img;
  5. });
  6. });
  1. Use async/await
  1. this.auth.msg.subscribe(async (res: any) =&gt; {
  2. let img = await this.fetchImage(this.user.id);
  3. this.url = img;
  4. });

While .toPromise() is deprecated from RxJS 7. Recommend migrating the .toPromise() with firstValueFrom/lastValueFrom as stated in the RxJS documentation:

  1. import { lastValueFrom } from &#39;rxjs&#39;;
  2. async fetchImage(Id)
  3. {
  4. let result: any;
  5. let payload = {
  6. Id: Id
  7. };
  8. result = await lastValueFrom(this.Sev.getPicture(payload));
  9. return result.response;
  10. }

答案2

得分: 1

你可以尝试使用 switchMap。

  1. this.auth.msg.pipe(
  2. switchMap(_ => this.fetchImage(this.user.id))
  3. ).subscribe((img) => {
  4. this.url = img;
  5. ...
  6. });
英文:

You could try switchMap

  1. this.auth.msg.pipe(
  2. switchMap(_ =&gt; this.fetchImage(this.user.id))
  3. ).subscribe((img) =&gt; {
  4. this.url=img;
  5. ...
  6. });

huangapple
  • 本文由 发表于 2023年5月13日 17:16:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76241959.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定