角度服务数据未更新

时间:2019-02-27 18:35:11

标签: angular

我有两种方法: getUserProfile 返回用户配置文件和 updateProfile 更新用户数据。
 UpdateProfile方法正常工作,因为它在数据库中显示有效的更新数据。我的getUserProfile方法有问题,因为调用updateProfile方法后,不会显示组件中的更新数据,而是显示更改KafkaConsumer之前的数据。仅当我注销该应用程序并再次登录时,它将显示更新的数据。 我认为问题出在localstorage和userToken值,因为注销后便清除了它。并且只有在重新登录后,getUserProfile方法才被再次调用并且可以正常工作。Data not updated correctly

组件类:

      export class AccountComponent implements OnInit {
      userData: any;
      profileForm: FormGroup;
      constructor(private formBuilder: FormBuilder, private authenticationService: AuthenticationService, private router: Router) { }
      ngOnInit() {
      this.formInitBuilder();
        this.authenticationService.getUserProfile().subscribe( (data: any) => {
          this.userData = data;
          this.profileForm.setValue({
            username: data.UserName,
            email: data.Email,
            firstName: data.FirstName,
            lastName: data.LastName
          });
          console.log(data);
        });

      }
      formInitBuilder() {
        this.profileForm = this.formBuilder.group ({
          username: [ '' ],
          email: [ '' ],
          firstName: [ '' ],
          lastName: [ '']
        });
      }
      onSubmit(registrationForm: FormGroup) {
        if (registrationForm.valid) {
        this.authenticationService.updateProfile(this.profileForm.value).
         subscribe(() => {
          this.router.navigate(['/home']);
          });
        }
      }
}

服务等级:

      getUserProfile() {
    return this.http.get(this.rootUrl + '/api/GetUserClaims',
    {headers: new HttpHeaders({'Authorization': 'Bearer ' + JSON.parse(localStorage.getItem('userToken'))})});
  }
  updateProfile(userData) {
    const profile = {
      Username: userData.username,
      Email: userData.email,
      FirstName: userData.firstName,
      LastName: userData.lastName,
    };
    const reqHeader = new HttpHeaders({'Content-Type': 'application/json'});
    return this.http.post(this.rootUrl + '/api/ChangeProfile', profile, {headers: reqHeader});
  }

网络Api:

    [HttpGet]
    [Route("api/GetUserClaims")]
    public AccountModel GetUserClaims()
    {
        var identityClaims = (ClaimsIdentity)User.Identity;
        IEnumerable<Claim> claims = identityClaims.Claims;
        AccountModel model = new AccountModel()
        {
            UserName = identityClaims.FindFirst("Username").Value,
            Email = identityClaims.FindFirst("Email").Value,
            FirstName = identityClaims.FindFirst("FirstName").Value,
            LastName = identityClaims.FindFirst("LastName").Value,
            LoggedOn = identityClaims.FindFirst("LoggedOn").Value
        };
        return model;
    }

1 个答案:

答案 0 :(得分:1)

我可能遗漏了一些东西,但是对您的api的调用仅进行了一次,除非您再次调用 getUserProfile ,否则不要期望您的数据被更新。刷新时,将执行 getUserProfile ,并使用 updateProfile 获取更新的数据。