Ben Racicot
Ben Racicot

Reputation: 5895

Detailed example of RxJs concatMap to get a user ID, then query a user with it

Having trouble getting concatMap to make the first query for a user id and then use the id for a second query.

This is how how the docs explain its usage but examples demonstrating this working don't seem to exist.

this.authenticationService.currentUser.pipe(
    concatMap((principal: Principal) => {
        this.getUser(principal.id: String)
    })
).subscribe((user: User) => {
    console.log(user);
});

This is all red in VSC.

[ts]
Argument of type '(principal: Principal) => void' is not assignable to parameter of type '(value: Principal, index: number) => ObservableInput<{}>'.
  Type 'void' is not assignable to type 'ObservableInput<{}>'. [2345]
(parameter) principal: Principal

Can someone help me make sense of this? I believe it may be because of TS 2.7+ no null returns checking.

Upvotes: 0

Views: 282

Answers (2)

Adrian Brand
Adrian Brand

Reputation: 21628

this.getUser(principal.id: String)

Is an invalid way to call a function, you only define the type of your parameter in the definition of the function parameters, not when calling the function.

It should be

this.getUser(principal.id)

Also your function does not return anything.

(principal: Principal) => {
  this.getUser(principal.id: String)
}

Returns void, you either need

(principal: Principal) => this.getUser(principal.id)

or

(principal: Principal) => {
  return this.getUser(principal.id: String)
}

Upvotes: 0

Maksim Romanenko
Maksim Romanenko

Reputation: 365

this.getUser(principal.id: String) doesn't seems to be valid typescript syntax, try this.getUser(principal.id as string); if type Principal has {id: string} definition, then just use this.getUser(principal.id)

Upvotes: 0

Related Questions