Monday, 3 April 2017

GET 404 (not found) - MEAN application

I'm currently trying to get user details (of the logged in user) on a profile page I'm building, but I receive this error:

GET http://localhost:3000/users/undefined 404 (Not Found)
error_handler.js:54 EXCEPTION: Response with status: 404 Not Found for 
URL: http://localhost:3000/users/undefined

I thought this could be an issue with stringifying the json data, but that doesn't seem to change anything, and I've read that Angular 2 no longer requires it.

I am using localhost:4200 for Angular, and localhost:3000 for Express. When I do an api call on localhost:3000/users/id (with the object id), it returns the json data just fine. I'm not sure what could be wrong.

Express route

router.get('/id', (req, res) => {
     console.log("getting a specific user");
     if(!mongoose.Types.ObjectId.isValid(req.params.id)) {
       return res.status(400).json({ message: 'Specified id is not valid' });
     }

     User.findById(req.params.id, (err, users) => {
       if (err) {
          return res.send(err);
        }

        return res.json(users);
      });


    });

Angular Service

import { Injectable } from '@angular/core';
import { AuthService } from './auth.service';
import { Http, Headers, RequestOptions, Response } from '@angular/http';

@Injectable()
export class UserService {

  BASE_URL: string = 'http://localhost:3000';

  constructor(
    private http: Http,
    private authService: AuthService

  ) { }

  get(id) {
  let headers = new Headers({ 'Authorization': 'JWT ' + this.authService.token });
  let options = new RequestOptions({ headers: headers });
  return this.http.get(`${this.BASE_URL}/users/${id}`, options)
    .map((res) => res.json());
}

Profile Component

import { Component, OnInit, Input } from '@angular/core';
import { AuthService } from '.././services/auth.service';
import { UserService } from '.././services/user.service'
import { Router, ActivatedRoute } from '@angular/router';


@Component({
  selector: 'app-profile',
  templateUrl: './profile.component.html',
  styleUrls: ['./profile.component.css'],
  providers: [UserService]
})
export class ProfileComponent implements OnInit {

  currentUser;

  isAuth: boolean;

  constructor(
    private session: AuthService,
    private router:  Router,
    private userService: UserService,
    private route: ActivatedRoute

  ) {


  this.session.isAuth
       .subscribe((isAuth: boolean) => {
       // user will be false if logged out
       // or user object if logged in.
         this.isAuth = isAuth;
       });
   if (this.session.token) {
     this.isAuth = true;
     console.log(this.session);
   } else {
     this.isAuth = false;
   }

  }




  ngOnInit() {

    this.route.params.subscribe(params => {
      this.getUserDetails(params['id']);
    });


  }

  getUserDetails(id) {
     this.userService.get(id)
       .subscribe((user) => {
         this.currentUser = user;
         console.log(this.currentUser);
       });
   }


  }



via universesurfer

No comments:

Post a Comment