从下拉列表中按下按钮后: 版
控制台中显示以下错误消息:
ERROR SyntaxError: Unexpected token < in JSON at position 0
at JSON.parse (<anonymous>)
at Response.webpackJsonp../node_modules/@angular/http/esm5/http.js.Body.json (http.js:1091)
at MapSubscriber.project (user.service.ts:30)
at MapSubscriber.webpackJsonp../node_modules/rxjs/_esm5/operators/map.js.MapSubscriber._next (map.js:79)
at MapSubscriber.webpackJsonp../node_modules/rxjs/_esm5/Subscriber.js.Subscriber.next (Subscriber.js:92)
at XMLHttpRequest.onLoad (http.js:1591)
at ZoneDelegate.webpackJsonp../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:421)
at Object.onInvokeTask (core.js:4740)
at ZoneDelegate.webpackJsonp../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:420)
at Zone.webpackJsonp../node_modules/zone.js/dist/zone.js.Zone.runTask (zone.js:188)
该错误导致该类方法: getUser : UserService
@Injectable()
export class UserService {
public jwtToken: string;
constructor(private http: Http) {
const theUser: any = JSON.parse(localStorage.getItem('currentUser'));
if (theUser) {
this.jwtToken = theUser.token;
}
}
getUser(userid) {
let headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('Authorization', `${this.jwtToken}`);
let options = new RequestOptions({headers: headers});
return this.http.get(`/user/${userid}`, options)
.map((response: Response) => response.json() );
}
}
班级代码 src / app / user / user-edition / user-edition.component.ts :
@Component({
selector: 'app-user-edition',
templateUrl: './user-edition.component.html',
styleUrls: ['./user-edition.component.css']
})
export class UserEditionComponent implements OnInit {
user: User;
UserType = UserType;
userObj: any;
profileForm: FormGroup;
constructor(private fb: FormBuilder,
private userService: UserService,
private router: Router,
private authService: AuthService) {}
username = new FormControl('', [Validators.required]);
password = new FormControl('', [Validators.required]);
email = new FormControl('', [Validators.required]);
type = new FormControl('', [Validators.required]);
description = new FormControl('', [Validators.required]);
street = new FormControl('', [Validators.required]);
houseNumber = new FormControl('', [Validators.required]);
city = new FormControl('', [Validators.required]);
ngOnInit() {
this.userObj = this.authService.currentUser;
this.profileForm = this.fb.group({
username: this.username,
password: this.password,
email: this.email,
type: this.type,
description: this.description,
street: this.street,
houseNumber: this.houseNumber,
city: this.city
});
this.userService.getUser(this.userObj.userid)
.subscribe(data => {
console.log(data);
if (data.success === false) {
if (data.errcode) {
this.authService.logout();
this.router.navigate(['/login']);
}
} else {
this.user = data.data[0];
this.populateForm(this.user);
}
});
}
populateForm(data): void {
this.profileForm.patchValue({
username: data.username,
password: data.password,
email: data.email,
type: data.type,
description: data.description,
street: data.street,
houseNumber: data.houseNumber,
city: data.city
});
}
edition() {
}
}
服务器代码: server / api.js
const express = require('express');
const router = express.Router();
var bodyParser = require('body-parser');
var morgan = require('morgan');
var mongoose = require('mongoose');
var jwt = require('jsonwebtoken');
var config = require('./config');
var user = require('../routes/user.js');
mongoose.connect(config.database, function(err){
if(err) {
console.log('Error connecting database, please check if MongoDB is running.');
} else { console.log('Connected to database...');
}
});
router.post('/login', user.login);
router.get('/user/:id', user.getuserDetails);
module.exports = router;
routes / user.js :
var mongoose = require( 'mongoose' );
var User = require('../models/user');
var Address = require('../models/user');
var jwt = require('jsonwebtoken');
var config = require('../server/config');
exports.getuserDetails = function(req, res, next){
User.find({_id:req.params.id}).exec(function(err, user){
if(err){
res.status(400).json({
success: false, message: 'Error processing request '+ err
}); }
res.status(201).json({
success: true,
data: user
});
});
}
我不知道导致的错误是什么。如何解决?
未调用 exports.getuserDetails 方法