Angular 5将数据从angular发送到express

时间:2018-04-19 05:27:15

标签: angular express mean

我使用subscribe将我的数据从angular发送到express,但它没有用。

这是来自angular的我的ProfileComponent.ts:

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

并且我的快递给用户提供了未定义的值。

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.log中调用所有用户数据时,它返回undefined。

1 个答案:

答案 0 :(得分:1)

您在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.log(user.id_student)因为不存在。

而是执行console.log(user.id)