Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ngrx store getting error as Property 'ofType' does not exist on type

I am trying to develop a application using ngrx/store getting error. unable to figureout the issue. any one help me?

here is my errors:

ERROR in src/app/store/effects/authentication.effects.ts(25,7): error TS2339: Property 'ofType' does not exist on type 'Actions<Action>'.
src/app/store/effects/authentication.effects.ts(29,59): error TS2339: Property 'email' does not exist on type '{}'.
src/app/store/effects/authentication.effects.ts(29,74): error TS2339: Property 'password' does not exist on type '{}'.
src/app/store/effects/authentication.effects.ts(33,73): error TS2339: Property 'email' does not exist on type '{}'.

typescript file :

import { Injectable } from '@angular/core';
import { Action } from '@ngrx/store';
import { Router } from '@angular/router';
import { Actions, Effect, ofType } from '@ngrx/effects';
import { Observable,  of } from 'rxjs';
import { map, switchMap, catchError, tap } from 'rxjs/operators';

import { AuthenticationService } from './../../service/authentication.service';
import { AuthenticationActionTypes, 
         Login, LoginSuccess, 
         LoginFailure, Logout } from './../actions/authentication.actions';

@Injectable({
    providedIn: 'root'
})
export class AuthenticationEffects {

    constructor(
        private actions:Actions, 
        private authenticationService:AuthenticationService,
        private router:Router) {}

    @Effect()
        Login: Observable<any> = this.actions
        .ofType(AuthenticationActionTypes.LOGIN)
        .pipe(
            map((action: Login) => action.payload),
            switchMap(payload => {
                return this.authenticationService.login(payload.email, payload.password)
            .pipe(
                map((user) => {
                console.log(user);
                return new LoginSuccess({token: user.token, email: payload.email});
            }),
            catchError((error) => {
                return of(new LoginFailure({ error: error }));
            }));
    }));

    @Effect({ dispatch: false })
        LoginSuccess: Observable<any> = this.actions.pipe(
        ofType(AuthenticationActionTypes.LOGIN_SUCCESS),
            tap((user) => {
              localStorage.setItem('token', user.payload.token);
              localStorage.setItem('email', user.payload.email);
              this.router.navigateByUrl('/');
            })
        );

    @Effect({ dispatch: false })
        LoginFailure: Observable<any> = this.actions.pipe(
            ofType(AuthenticationActionTypes.LOGIN_FAILURE)
        );

    @Effect({ dispatch: false })
        public Logout: Observable<any> = this.actions.pipe(
            ofType(AuthenticationActionTypes.LOGOUT),
                tap((user) => {
            localStorage.removeItem('token');
            localStorage.removeItem('email');
            this.router.navigateByUrl('/login');
        })
    );

}

JSON File :

{
  "name": "authentication",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "~7.2.0",
    "@angular/common": "~7.2.0",
    "@angular/compiler": "~7.2.0",
    "@angular/core": "~7.2.0",
    "@angular/forms": "~7.2.0",
    "@angular/platform-browser": "~7.2.0",
    "@angular/platform-browser-dynamic": "~7.2.0",
    "@angular/router": "~7.2.0",
    "@ngrx/core": "^1.2.0",
    "@ngrx/effects": "^7.0.0",
    "@ngrx/store": "^7.0.0",
    "core-js": "^2.5.4",
    "rxjs": "~6.3.3",
    "tslib": "^1.9.0",
    "zone.js": "~0.8.26"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.12.0",
    "@angular/cli": "~7.2.2",
    "@angular/compiler-cli": "~7.2.0",
    "@angular/language-service": "~7.2.0",
    "@types/node": "~8.9.4",
    "@types/jasmine": "~2.8.8",
    "@types/jasminewd2": "~2.0.3",
    "codelyzer": "~4.5.0",
    "jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~3.1.1",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.1",
    "karma-jasmine": "~1.1.2",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.4.0",
    "ts-node": "~7.0.0",
    "tslint": "~5.11.0",
    "typescript": "~3.2.2"
  }
}

any one help me here.... thanks in advance.

like image 744
3gwebtrain Avatar asked Jan 17 '19 16:01

3gwebtrain


People also ask

How to solve'property does not exist on type {}'error?

The "Property does not exist on type ' {}'" error occurs when we try to access or set a property that is not contained in the object's type. To solve the error, type the object properties explicitly or use a type with variable key names. Copied!

What happened to the oftype function in ngrx?

– Konstantin Malikov Feb 4 '20 at 20:56 1 In NgRx 6.1 the ofType function was marked as deprecated in favor of the ofType operator, in NgRx v7 this function was dropped.

What is the difference between ngrx store and ngrx effects?

NgRx Store provides reactive state management for Angular apps inspired by Redux. Unify the events in your application and derive state using RxJS. NgRx Effects gives you a framework for isolating side effects from your components by connecting observables of actions to your store.

Why can't I assign properties to an empty object?

We didn't explicitly type the obj variable and declared it to an empty object, so we aren't able to assign or access properties that don't exist on the object's type.


1 Answers

within your first effect you didn't put ofType into pipe:

so, instead of:

Login: Observable<any> = this.actions
        .ofType(AuthenticationActionTypes.LOGIN)
        .pipe(

do:

Login: Observable<any> = this.actions.pipe(
        ofType(AuthenticationActionTypes.LOGIN)
like image 86
dee zg Avatar answered Oct 08 '22 23:10

dee zg