Encountering a mistake that can be assigned to a type parameter

Within the code block below, I am utilizing "rxjs": "~6.2.0"

    canActivate():Observable<boolean>
     {
      return this.auth.user$.pipe
      (
        switchMap( 
                 user => 
                   {
                   return this.userService.get(user.uid);
                   }
                 )
       ).pipe( map( appUser => appUser.isAdmin))
     }

The get method within userService contains the following logic.

    get(uid : string): AngularFireObject<AppUser>
      {
        return this.db.object('/users/'+ uid);
      }

A specific error is being encountered

"Argument of type '(user: User) => AngularFireObject' is not assignable to parameter of type '(value: User, index: number) => ObservableInput'. Type 'AngularFireObject' is not assignable to type 'ObservableInput'. Type 'AngularFireObject' is not assignable to type 'Iterable'. Property '[Symbol.iterator]' is missing in type 'AngularFireObject'."

Answer №1

Ensure that you do not need to use two pipes in this scenario, instead, map the outcome of appUser.isAdmin using a single pipe.

canActivate():Observable<boolean>
{
      return this.auth.user$
       .pipe(
           switchMap(user => this.userService.get(user.uid)),
           map(appUser => appUser.isAdmin)
       )
}

Furthermore, it is recommended to adjust userService.get() to return an appropriate Observable obtained from valueChanges().

get(uid : string): Observable<any>
{
     return this.db.object('/users/'+ uid).valueChanges();
}

Answer №2

The function argument passed into SwitchMap needs to be an Observable, however, the return value of this.userService.get(user.uid) is an AngularFireObject<AppUser>.

AngularFireObject comes from AngularFire library. I am not familiar with AngularFire, but it seems like you need to use valueChanges() method on the AngularFireObject to get an Observer. So if possible, update the UserService method as shown below:

get(uid : string): Observable<AppUser>
{
    return this.db.object('/users/'+ uid).valueChanges();
}

Also, make sure to combine the switchMap and map operators in the same pipe, and simplify the arrow function within switchMap as suggested by Amit Chigadani.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using Typescript generics to deduce the type from the second parameter for utilization in the first slot

Here is the code snippet I'm working with: export type Subscribe<T extends object> = <U>( listener: (slice: U) => void, selector: (state: T) => U, ) => void // The actual implementation is not relevant const subscribe = {} as ...

Troubleshooting common issues while setting up React Native with TypeScript

After carefully following the steps outlined in this guide on configuring a React Native project using TypeScript: https://facebook.github.io/react-native/blog/2018/05/07/using-typescript-with-react-native, I encountered a total of fifteen errors from the ...

Saving JSON Data in a Variable using Angular 2

Currently, I am making an HTTP GET request to fetch JSON data from my server. This includes two objects - minutes and seconds - which are consistently changing values as they represent a timer. While I successfully interpolate the value in the HTML templat ...

Is there a way for me to bypass adding the label if it already exists in my state, but still include the new value?

I am currently facing an issue with a dropdown select field where I can choose various attribute values. The problem arises when attempting to save the selected data in the state, as the label appears twice and I only require it once. My goal is to exclude ...

In the realm of JavaScript, the localeCompare() string method is more than willing to accept the presence of 'undefined' as a valid parameter for 'locale', while opting to outright reject any instance of

I have discovered a method to sort strings naturally const rows = ['37-SK', '4-ML', '41-NP', '2-YZ', '21', '26-BF']; console.log(rows.sort((a, b) => a.localeCompare(b, undefined, { numeric: tru ...

The default values for CSS filters

I have a keen interest in various CSS filters: blur brightness contrast grayscale hue-rotate invert opacity saturate sepia Could someone provide the default values for each filter (preferably as a % value, where applicable)? The MDN documentation does no ...

How can you determine the data types of properties within a blank class object using Typescript and Angular?

Class Example{ constructor( public x: number, public y: string ) {} } let e = new Example(); Is there a way to programmatically determine the data type of e.x or e.y in TypeScript? ...

How can I dynamically insert various FormGroup instances into a FormArray in Angular?

I am looking to dynamically populate the order array with multiple dishes. Each dish will be stored as a FormGroup in the form, such as pizza, salad, drink, or any other type of dish. Prior to adding any items, the form structure should resemble this: this ...

Is it possible to create two subclasses where the methods return the types of each other?

I am faced with a situation where I have two classes that rely on each other's methods: class City { (...) mayor(): Person { return this.people[0]; } } class Person { (...) birthCity(): City { return this.cities.birth; } } ...

Step-by-step guide on transitioning code to utilize @use instead of @import

Currently, I am in the process of revamping a clubs app from Angular 9 to 18, as it has not been well maintained. My goal is to preserve as much of the frontend as possible and maintain the design more or less the same. Within the code, I have identified ...

Determining type properties dynamically depending on the value of another property

My goal is to create a type that ensures the correct usage of the DynamicColor type. enum ColorsEnum { red = "red", green = "green", blue = "blue", yellow = "yellow", } type ColorsMapperType = { type: Colo ...

The attribute 'date' is not found within the class 'EmployeeScheduleExceptionModel', however, it is present in the parent class from which it inherits

I am working on a TypeScript project and I have defined my configurations in the tsconfig.json file as shown below: { "include": ["src*"], "compilerOptions": { "target": "es2021", &q ...

Is there any distinction between using glob wildcards in the tsconfig.json file when specifying "include" as "src" versus "include" as "src/**/*"?

Is there a distinction between these two entries in the tsconfig.json file? "include": ["src"] "include": ["src/**/*"] Most examples I've come across use the second version, but upon reviewing my repository, ...

How can I customize ngx-quill editor's link behavior to open in the same tab instead of a new tab?

Incorporating Quill's rich editor with Angular 4 leads to the issue of links being automatically set to open in a new tab due to "_target = "blank". Is there a way to have the links open in the same tab instead? Any guidance on this matter would be gr ...

Unable to set items as sticky within mat-tabs

Is there a way to make my image stick in place using the following CSS code? position: -webkit-sticky; position: sticky; top: 1px; I have tried implementing it but haven't been successful. HTML: <mat-tab-group class="user-tabs" (selectedTa ...

Asserting types for promises with more than one possible return value

Struggling with type assertions when dealing with multiple promise return types? Check out this simplified code snippet: interface SimpleResponseType { key1: string }; interface SimpleResponseType2 { property1: string property2: number }; inter ...

What causes a BehaviorSubject to update when modifying the [(NgModel)] values of a form without actually submitting the form?

When I work with a form that relies on a BehaviorSubject to load user information, I noticed that if I make changes to the inputs but navigate away from the page without submitting the form, the user information gets updated in the subject automatically. ...

Angular2 ngIf directive issue after initial button click

I have recently started using the Angular2 Framework, so please bear with me if I make any common mistakes. I have set up two forms in separate div tags: one for logging in and another for password reset. Below the login form, there is a link that, when cl ...

The process of ordering awaits using an asynchronous method

async fetchAndStoreRecords(): Promise<Records[]> { this.subRecords = await this.afs.collection<Records>('records') .valueChanges() .subscribe((data: Records[]) => { console.log('log before data ...

Using AngularFire2 to manage your data services?

After diving into the resources provided by Angular.io and the Git Docs for AngularFire2, I decided to experiment with a more efficient approach. It seems that creating a service is recommended when working with the same data across different components in ...