0

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.

Ben Racicot
  • 5,332
  • 12
  • 66
  • 130
  • Thats telling you that you need to return from `cancatMap()`. Do ` return this.getUser(principal.id: String)` – Ashish Ranjan Dec 15 '18 at 19:37
  • Fat arrows have implicit returns and automatically are returning `this.getUser(principal.id: String)` – Ben Racicot Dec 15 '18 at 19:47
  • 2
    They have implicit return if you don't add `"{" ... "}"` . So your code should be `concatMap((principal: Principal) => this.getUser(principal.id: String))` – Ashish Ranjan Dec 15 '18 at 19:51
  • 1
    Awesome yes, I've tried that and the original error is gone (queries working). Now I believe TS' 'no null checks' are blowing it up because `principle.id` is undefined until the first return. Thanks for following through and helping. – Ben Racicot Dec 15 '18 at 19:55
  • You are welcome :) – Ashish Ranjan Dec 15 '18 at 19:57

2 Answers2

0

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)

0
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)
}
Adrian Brand
  • 20,384
  • 4
  • 39
  • 60