首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >日志解密变量内容角7

日志解密变量内容角7
EN

Stack Overflow用户
提问于 2020-04-01 03:33:50
回答 1查看 16关注 0票数 0

我有一个名为currentUser的声明变量,它包含一个来自http get请求的用户对象,我只想记录currentUser变量,但我没有定义!有人能解释一下为什么吗?

代码语言:javascript
复制
import { Component, OnInit, NgModule } from '@angular/core';
import { HttpClient } from '@angular/common/http';    
import { FormGroup, FormControl, FormBuilder, Validators } from '@angular/forms';

@Component({
  selector: 'app-profile',
  templateUrl: './profile.component.html',
  styleUrls: ['./profile.component.css']
})
export class ProfileComponent implements OnInit {
  currentUser: any;
  user: FormGroup;
  cities: any[];
  fileToUpload: any;
  avatarPath: any;
  avatarURL: any;
  returnUrl: any;

  constructor(
    private http: HttpClient,
    private fb:FormBuilder
  ) {
   this.createForm();
  }

  ngOnInit() {

    this.http.get('api/user').subscribe(user => {
      this.currentUser = user;
      localStorage.setItem('currentUser', JSON.stringify(this.currentUser));
    });
    console.log('this.currentuser :', this.currentUser)    
    this.http.get('api/cities').subscribe((cities: any[]) => {
      this.cities = cities;
    });
    this.user.patchValue({
      firstName: this.currentUser.firstName,
      lastName: this.currentUser.lastName,
      email: this.currentUser.email,
      profession: this.currentUser.profession,
      gender: this.currentUser.gender,
      birthDate: this.currentUser.birthDate,
      phone: this.currentUser.phone,
      oldPassword: this.currentUser.oldPassword,
      password: this.currentUser.newPassword,
      address: this.currentUser.address,
      postalCode: this.currentUser.postalCode,
      city: this.currentUser.city._id
    });
  }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-01 03:52:36

我认为它的发生是因为同步。试试这个:

代码语言:javascript
复制
this.http.get('api/user').subscribe(user => {
    this.currentUser = user;
    console.log('this.currentuser :', this.currentUser);
    localStorage.setItem('currentUser', JSON.stringify(this.currentUser));
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60958482

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档