0

I'm using for the first time switchMap. It's telling me to return "something", but when I put a return it doesn't work.

 @Effect()
  subData$ = this.actions$.pipe(
    ofType(ActionTypes.SEARCH_SUB_DATA),
    map(action => action['payload']),
    switchMap(payload => { //Payload gets highlighted in red
      const repos = this.githubSearch.getRepos(payload);
      const followers = this.githubSearch.getFollowers(payload);
      const starred = this.githubSearch.getStarred(payload);
      return forkJoin([repos, followers, starred]).subscribe(results => {
        this.store.dispatch(
          new UserActions.SuccessSubData({
            user: payload,
            repos: results[0],
            followers: results[1],
            starred: results[2]
          })
        );
        return of(results);
      });
    })
  );

Argument of type '(payload: never) => Subscription' is not assignable to parameter of type '(value: never, index: number) => ObservableInput<{}>'. Type 'Subscription' is not assignable to type 'ObservableInput<{}>'.

UPDATE BASED ON ANSWER PROVIDED

@Effect()
      subData$ = this.actions$.pipe(
        ofType(ActionTypes.SEARCH_SUB_DATA),
        map(action => action['payload']),
        switchMap(payload => {
          return forkJoin(
            this.githubSearch.getRepos(payload),
            this.githubSearch.getFollowers(payload)
          );
        }).subscribe(results => {
          this.store.dispatch(
            new UserActions.SuccessSubData({
              user: payload,
              repos: results[0],
              followers: results[1]
            })
          );
        })
      );

Here's the picture showing the errors

image from error

2 Answers 2

1
@Effect()
  subData$ = this.actions$.pipe(
    ofType(ActionTypes.SEARCH_SUB_DATA),
    map(action => action['payload']),
    switchMap(payload => {
      return forkJoin(
        this.githubSearch.getRepos(payload),
        this.githubSearch.getFollowers(payload)
      ).pipe(map(([repos,followers]) => //return success action here), catchError(error => of(//return failaction))
    })
  )

Usually you don't have to subscribe, it will just return the stream and let ngrx handle the subscriptiona and unsubscription. SO here I am mapping success and failure callback actions. They will be automatically triggered by ngrx.

Sign up to request clarification or add additional context in comments.

1 Comment

Glad you found it useful.
0

You are trying to merge an Observable with a Subscription and also when use forkJoin you are using with two undefined values. Code below must work for your case.

@Effect()
  subData$ = this.actions$.pipe(
    ofType(ActionTypes.SEARCH_SUB_DATA),
    map(action => action['payload']),
    switchMap(payload => {
      return forkJoin(
        this.githubSearch.getRepos(payload),
        this.githubSearch.getFollowers(payload)
      );
    })
  ).subscribe(results => {
      this.store.dispatch(
        new UserActions.SuccessSubData({
          user: payload,
          repos: results[0],
          followers: results[1]
        })
      );
    });

8 Comments

Hola parce! thanks for the help. This didn't work. No every word 'payload' things is an object instead of a string and the .subscribre() is in red because aparently switchmap doesnt return an observable
Yeah my bad, I edited the answer, I am pretty sure it must work now.
still same issue. Idk if it makes a difference that I have the switchMap inside of an effect. I have updated my questions with your code and attached an image
I just edited again, it turns out you have get rid of brackets in forkJoin.
same error :( I have updated my questions with the code without brakets and udpated image too
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.