Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
249 views
in Technique[技术] by (71.8m points)

angular - Error: Type 'string' is not assignable to type 'Subscription'. How can I convert/parse this?

I am trying to get a simple string from my database using an Observable. I've done it like this before and haven't had any problems.

Configuration is a simple object with just an id: number and a name: string.

I'm calling this function from a ForkJoin.

Here is my code:

  private getMaxSeconds() {

  let maxSeconds = this.configurationRemoteService.get('MAX_SECONDS_VIDEOGRAMMETRY_SMALL')
    .subscribe(config => {
      maxSeconds = config.value;
    }, error => console.log(error));

    return maxSeconds;
  }

ForkJoin code(reduced version):

forkJoin([
        this.getMaxSeconds(),
      ]).subscribe(response => {
        this.MaxSeconds= response[0] as string;
});

But this time, I'm getting this error: Type 'string' is not assignable to type 'Subscription'.

The only difference is that now I am declaring the variable with let in the function.

How can I convert or parse this Suscription into a string?

I'm using TypeScript 4, Angular 8 and Laravel 4 for the API.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You aren't declaring the variable in the function but straight up initializing it with the subscription. Instead you need to declare it. Try the following

private getMaxSeconds() {
  let maxSeconds: string;

  this.configurationRemoteService.get('MAX_SECONDS').subscribe(
    config => {
      maxSeconds = config.value;
   },
    err => { }
  );

  return maxSeconds;     // <-- WRONG! Will return `undefined`

And you cannot return the async variable maxSeconds synchronously like you're trying. By the time the return statement is executed, maxSeconds wouldn't be initialized yet and it'll return undefined. Instead any statements that depend on the async call get('MAX_SECONDS') should be inside the subscription.

You could find more information about asynchronous data here.

Update: use with forkJoin

RxJS forkJoin takes in observables as arguments, so can directly use this.configurationRemoteService.get('MAX_SECONDS'). And if you wish to use only the value from it's notification, you can pipe in a map to it.

Try the following

forkJoin(
  this.configurationRemoteService.get('MAX_SECONDS').pipe(map(res => res['value'])),
  // 2nd observable,
  // 3rd observable
).subscribe(
  ([value, res2, res3]) => {
    console.log(value);    // <-- `value` property from `this.configurationRemoteService.get('MAX_SECONDS')`
    console.log(res2);     // <-- notification from 2nd observable argument
    console.log(res3);     // <-- notification from 3rd observable argument
  },
  err => { }
);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...