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



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

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

    });

角度服务

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

  }

请使用以下代码:

快速路线

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

角度服务

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

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

相关内容

  • 没有找到相关文章

最新更新