Node.js 如何解决应用程序中的GET 404(未找到)错误?
我当前正试图在我正在构建的配置文件页面上获取(登录用户的)用户详细信息,但收到以下错误:Node.js 如何解决应用程序中的GET 404(未找到)错误?,node.js,angular,express,angular2-routing,mean-stack,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数据的一个问题,但这似乎没有改变
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());
}
我希望这能满足您的需要。谢谢您的建议。很遗憾,我收到了相同的错误:/您是否在cmd中收到此消息“getting a specific user”(获取特定用户)?这是一个404(未找到)错误。目前我最好的猜测是,我没有正确地获取Angular中的.u id参数,因为它确实在服务器中工作。但我没有在cmd中收到任何“获取特定用户”消息。我发现,如果我手动将id输入url,我可以在Angular 2配置文件页面上检索经过身份验证的用户数据。看起来我需要将id传递到我的[routerLink]配置文件url中。但那是在我的导航部分。这意味着(当前)我的用户信息不可用于我的导航组件。我知道一定有更好的办法。