Angular 5将数据从Angular发送到express
我会使用subscribe将我的数据从angular发送到express,但它不起作用 这是angular的my ProfileComponent.ts:Angular 5将数据从Angular发送到express,angular,express,mean,Angular,Express,Mean,我会使用subscribe将我的数据从angular发送到express,但它不起作用 这是angular的my ProfileComponent.ts: updateProfileCompany(e){ var getUser = localStorage.getItem('user'); getUser = JSON.parse(getUser) var id_student = getUser["id"]; var nim = getUse
updateProfileCompany(e){
var getUser = localStorage.getItem('user');
getUser = JSON.parse(getUser)
var id_student = getUser["id"];
var nim = getUser["nim"];
var nama = getUser["nama"];
var email = getUser["email"];
var id_company = this.company;
const user = {
id : id_student,
nama : nama,
email : email,
nim : nim
}
console.log(user);
this.authService.updateProfileCompany(user)
.subscribe((res:Response) => this.user = user);
}
这是我的authService.ts:
updateProfileCompany(user){
let headers = new Headers();
this.loadToken();
var users = localStorage.getItem(this.user);
headers.append('Authorization', this.authToken);
headers.append('Content-Type', 'application/json')
return this.http.put('http://192.168.100.10:3000/users/update', user, {headers: headers})
.map(res => res.json());
}
在我的express中,给出用户未定义的值
router.put('/update', passport.authenticate('jwt', {session:false}), (req, res, next)=>{
user = req.user;
var id_student = user.id;
var id_company = req.body.company;
var result = [];
console.log(user.id_student);
});
当我尝试从angular中console.log所有用户数据时,它返回undefined。您在ajax请求中发送的用户对象位于req.body对象中
router.put('/update', passport.authenticate('jwt', {session:false}),(req, res, next)=>{
user = req.body; // here
var id_student = user.id;
// var id_company = req.body.company; this you should add to the object that you are sending
var result = [];
console.log(user.id);
});
发送的用户对象具有此结构
const user = {
id : id_student,
nama : nama,
email : email,
nim : nim
}
所以你不能做console.loguser.id\u学生,因为不在那里
相反,请执行console.loguser.id未定义的路由是在哪里被调用的?const user={id:id_student,nama:nama,email:email,nim:nim,id_company:id company}但如果我是console.log id_company,它将返回未定义的console.loguser.id_company,它将返回未定义的