如何解决MEAN应用程序中的GET 404(未找到)错误?

时间:2017-04-03 20:47:52

标签: node.js angular express angular2-routing mean-stack

我目前正在尝试在我正在构建的个人资料页面上获取用户详细信息(已登录用户),但我收到此错误:

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

我认为这可能是字符串化json数据的问题,但这似乎没有改变任何东西,我读过Angular 2不再需要它。

我使用localhost:4200用于Angular,localhost:3000用于Express。当我在localhost:3000 / users / id(带有对象id)上进行api调用时,它返回json数据就好了。我不确定会出现什么问题。

快速路线

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());
}

个人资料组件

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);
       });
   }


  }

1 个答案:

答案 0 :(得分:0)

请使用以下代码:

快速路线

free

Angular Service

router.get('/users/: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' });
 }
 else {
 User.findById(req.params.id, (err, users) => {
 if (err) {
    return res.send(err);
 }
 else {
    return res.json(users);
 }
});
}
});

我希望,这可以根据您的需要运作。