Angular 5将对象从Angular发送到Express Route

时间:2018-04-20 02:00:50

标签: angular mean

我想将用户对象从angular发送到express,但是当我尝试在express中使用console.log时,它返回undefined。 这是我在ts文件中的方法:

  updateProfileCompany(){
    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 company    = this.company
    const user = {
      id         : id_student,
      nama       : nama,
      email      : email,
      nim        : nim,
      id_company : company
    }
    console.log(user.id_company); // This is work
    this.authService.updateProfileCompany(user)
    .subscribe((res:Response) => this.user = user);
  }

这是我的明确路线:

router.put('/update', passport.authenticate('jwt', {session:false}), (req, res, next)=>{
    user = req.user;
    var result = [];
    console.log(user.id_company); // returns undefined
});

1 个答案:

答案 0 :(得分:0)

您的对象中没有 id_company ,我认为您需要 _id

样本



var user  ={"company":[],"_id":"5ad72254452996029415e49f","nama":"Muhammad Fatih Rizqon","email":"fatih@me.id","nim":"14520111003","password":"$2a$10$n1jbglV3F0RRvTkSVGFNSOrJ.y9A/7zZkVsCq.BUmmTzSg7pChykK","__v":1};
console.log(user._id);