0

I have this error when I try to login to my app but i have some problems with a 404(not found) included... I will put some lines of my code and the image of the error..... Please help me fix the issue .

@Injectable()
export class AuthGuard implements CanActivateChild {
  loggedInUser: string;

  constructor( private localStorage: LocalStorageService, private router: Router ) {
    this.loggedInUser = localStorage.get('discogs-user') as string;
  }

  canActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
    if (this.loggedInUser) {
      return true;
    }

    this.router.navigate(['/login']);
  }
}

this is the login.component.ts with the subscribe

import { Component } from '@angular/core';
import { Store } from '@ngrx/store';
import { Router } from '@angular/router';

import { Observable } from 'rxjs/Observable';

import { MdlSnackbarService } from 'angular2-mdl';

import * as fromRoot from '../../reducers';
import * as user from '../../actions/user';

import { DiscogsUser, UserLogin } from '../../models';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html'
})
export class LoginComponent {
  user$: Observable<DiscogsUser>;

  login(login: UserLogin) {
    if (!login.username) {
      this.store.dispatch(new user.LoginFailedAction('You must enter a username'));
      return;
    }

    this.store.dispatch(new user.LoginAction(login));
    this.router.navigate(['/']);
  }

  private _showError(message: string) {
    this.mdlSnackbarService.showSnackbar({
      message: message,
      action: {
        handler: () => { },
        text: 'OK'
      }
    });
  }

  constructor(private store: Store<fromRoot.State>, private router: Router, private mdlSnackbarService: MdlSnackbarService) {
      this.user$ = store.select(fromRoot.getUser);

      store.select(fromRoot.getLoginFailed)
        .subscribe(error => {
          if (error) {
            this._showError(error);
          }
        });

    }
}

this is the image of the error

error

  • Please do not use the angularjs tag on Angular 2+ questions. Thanks. – Mike Feltman Jul 25 '17 at 18:08
  • I'd suggest you clip the RHS of the image, so the image will render better at a larger font size. The web service call causes the 404 - it's saying there's nothing at the URL. So the URL with maximendez if you pasted that in browser URL, it would yield nothing but a not found. You are probably not error handling in a subscribe to return an empty object as your JSON payload. – JGFMK Jul 25 '17 at 18:17
  • subscribe(data => this.data = data.json, err =>{ this.data = {}; console.error(error)}) – JGFMK Jul 25 '17 at 18:20
  • @MikeFeltman Is it not Angular 2+? https://angular.io/guide/router#canactivatechild-guarding-child-routes – JGFMK Jul 25 '17 at 18:27
  • The OP specified Angular 2 in the subject, so I hope it is. I removed the angularjs tag from the question. I'm just adding a comment when I do so in order to help raise awareness on the intended use of the angularjs and angular tags. – Mike Feltman Jul 25 '17 at 18:29
  • what is the problem actually? the fact that server returns 404 or the fact that angular doesn't handle an error properly? – dee zg Jul 25 '17 at 19:26
  • the problem is the 404 error... i dont know what is wrong in the code.... i think there're some problems with the service... – Maxi Mendez Jul 25 '17 at 19:27
  • You have an issue calling url `/api/user/[username]` but you do not include the code that makes the call OR anything about how the server is implemented and how it should respond to that route? Please read through how to add an [mcve] and [ask] a good question if you want further help, any answer without more information would be speculation at best. – Igor Jul 25 '17 at 19:34

0 Answers0