简体   繁体   English

无法访问 typeScript object 值

[英]unable to access typeScript object value

I am working in Angular 8. I have a model class named Basic , to hold the basic information of a profile.我在 Angular 8 工作。我有一个名为Basic的 model class 来保存配置文件的基本信息。 In my component, a object of Basic type will be returned from Service , the service will get values from server.在我的组件中,一个Basic类型的 object 将从Service返回,该服务将从服务器获取值。 In my component when i am printing the object of Basic , it is showing that there is values, but when i am trying to access those values it is showing undefined.在我的组件中,当我打印Basic的 object 时,它显示存在值,但是当我尝试访问这些值时,它显示未定义。 My Basic class has getter/setter.我的Basic class 有 getter/setter。

Service Code服务代码

bas: Basic = new Basic();

  public getCurrentUserBasic() {
    return this.http.get<Basic>(
      'http://127.0.0.1:8080/public/profile/basic/findOneByUser', this.authService.getHeader(),
    ).subscribe((b: Basic) => {
      this.loading = false;
      console.log(b);

      // phone number
      for (const key in b['phone_number']) {
        const phoneNumber = new PhoneNumber();
        phoneNumber.$id = b['phone_number'][key]['id'];
        phoneNumber.$number = b['phone_number'][key]['number'];
        this.phoneNumbers.push(phoneNumber);
      }

      // basic information
      this.bas.$id = b['id'];
      this.bas.$name = b['name'];
      this.bas.$birthDate = b['birthDate'];
      this.bas.$gender = b['gender'];
      this.bas.$blood_Group = b['blood_Group'];
      this.bas.$religion = b['religion'];
      this.bas.$email = b['email'];
    });
  }// get current user basic.


public getBasicInformation() {
    return this.bas;
  }

Component零件

public setBasicInformation() {
    try {
      var basic: Basic = this.basicService.getBasicInformation();
      this.name = basic.$name;
      console.log(this.name);
      console.log('\n\n name : ' + basic['name']);
      console.log('\n\n $name : ' + basic.$name);
      this.birthDate = basic.$birthDate;
      this.gender = basic.$gender;
      console.log(basic);
    } catch (e) {
      console.log('Error in Setting Basic Information!');
    }
  }

The below lines are printing undefined .以下行正在打印undefined

console.log(this.name);// should not work, as property is private in model class.
console.log('\n\n name : ' + basic['name']);
console.log('\n\n $name : ' + basic.$name);

While this console.log(basic);虽然这个console.log(basic); line's result is below:行的结果如下: 基本结果

Model Class Model Class

export class Basic {
  private id: string;
  private userId: string;
  private name: string;
  private birthDate: string;
  private care_of: string;
  private gender: string;
  private maritalStatus: string;
  private profession: string;
  private blood_Group: string;
  private available: string;
  private religion: string;
  private email: string;

  /**
   * Getter $id
   * @return {string}
   */
  public get $id(): string {
    return this.id;
  }

  /**
   * Getter $userId
   * @return {string}
   */
  public get $userId(): string {
    return this.userId;
  }

  /**
   * Getter $name
   * @return {string}
   */
  public get $name(): string {
    return this.name;
  }

  /**
   * Getter $birthDate
   * @return {string}
   */
  public get $birthDate(): string {
    return this.birthDate;
  }

  /**
   * Getter $care_of
   * @return {string}
   */
  public get $care_of(): string {
    return this.care_of;
  }

  /**
   * Getter $gender
   * @return {string}
   */
  public get $gender(): string {
    return this.gender;
  }

  /**
   * Getter $maritalStatus
   * @return {string}
   */
  public get $maritalStatus(): string {
    return this.maritalStatus;
  }

  /**
   * Getter $profession
   * @return {string}
   */
  public get $profession(): string {
    return this.profession;
  }

  /**
   * Getter $blood_Group
   * @return {string}
   */
  public get $blood_Group(): string {
    return this.blood_Group;
  }

  /**
   * Getter $available
   * @return {string}
   */
  public get $available(): string {
    return this.available;
  }

  /**
   * Setter $id
   * @param {string} value
   */
  public set $id(value: string) {
    this.id = value;
  }

  /**
   * Setter $userId
   * @param {string} value
   */
  public set $userId(value: string) {
    this.userId = value;
  }

  /**
   * Setter $name
   * @param {string} value
   */
  public set $name(value: string) {
    this.name = value;
  }

  /**
   * Setter $birthDate
   * @param {string} value
   */
  public set $birthDate(value: string) {
    this.birthDate = value;
  }

  /**
   * Setter $care_of
   * @param {string} value
   */
  public set $care_of(value: string) {
    this.care_of = value;
  }

  /**
   * Setter $gender
   * @param {string} value
   */
  public set $gender(value: string) {
    this.gender = value;
  }

  /**
   * Setter $maritalStatus
   * @param {string} value
   */
  public set $maritalStatus(value: string) {
    this.maritalStatus = value;
  }

  /**
   * Setter $profession
   * @param {string} value
   */
  public set $profession(value: string) {
    this.profession = value;
  }

  /**
   * Setter $blood_Group
   * @param {string} value
   */
  public set $blood_Group(value: string) {
    this.blood_Group = value;
  }

  /**
   * Setter $available
   * @param {string} value
   */
  public set $available(value: string) {
    this.available = value;
  }


  /**
   * Getter $religion
   * @return {string}
   */
  public get $religion(): string {
    return this.religion;
  }

  /**
   * Setter $religion
   * @param {string} value
   */
  public set $religion(value: string) {
    this.religion = value;
  }

  /**
   * Getter $email
   * @return {string}
   */
  public get $email(): string {
    return this.email;
  }

  /**
   * Setter $email
   * @param {string} value
   */
  public set $email(value: string) {
    this.email = value;
  }

}//class

According to my knowledge these two lines should print values:据我所知,这两行应该打印值:

console.log('\n\n name : ' + basic['name']);
console.log('\n\n $name : ' + basic.$name);

while this console.log(basic);而这个console.log(basic); is printing values.正在打印值。

I think you are invoking setBasicInformation() before server has returned with the response in your getCurrentUserBasic() function.我认为你在服务器返回之前调用setBasicInformation()并在你的getCurrentUserBasic() function 中返回响应。

In Javascript objects are passed by reference, your var basic in setBasicInformation() refers to this.bas from your service.在 Javascript 对象通过引用传递,您在setBasicInformation()中的var basic引用了您的服务中的this.bas

When you a log an object on the console it will update the value of log when base object updates eg console.log (basic) will start reflecting the value of this.bas even on console as soon as it is updated in the service.当您在控制台上记录 object 时,它将在基本 object 更新时更新 log 的值,例如 console.log(基本)将开始反映 this.bas 的值,即使它在服务中更新后也会立即在控制台上反映。

While same is not the case when console.log for strings, it will always log the absolute value at the time of console.log being invoked.虽然对于字符串的 console.log 情况并非如此,但它始终会在调用 console.log 时记录绝对值。

Following two lines log strings at the time where value of basic is null, hence they are logging undefined.以下两行在基本值为 null 时记录字符串,因此它们记录未定义。

console.log('\n\n name : ' + basic['name']);
console.log('\n\n $name : ' + basic.$name);

Change your setBasicInformation() to set values in the subscribe fn of getCurrentUserBasic and pipe the values through getBasicInformation()更改您的setBasicInformation()以在getCurrentUserBasic和 pipe 的订阅 fn 中通过getBasicInformation()设置值


public getCurrentUserBasic() {
      return this.http.get<Basic>(
        'http://127.0.0.1:8080/public/profile/basic/findOneByUser', this.authService.getHeader())
    }


  public getBasicInformation( b ) {
        // phone number
        for (const key in b['phone_number']) {
          const phoneNumber = new PhoneNumber();
          phoneNumber.$id = b['phone_number'][key]['id'];
          phoneNumber.$number = b['phone_number'][key]['number'];
          this.phoneNumbers.push(phoneNumber);
        }

        // basic information
        this.bas.$id = b['id'];
        this.bas.$name = b['name'];
        this.bas.$birthDate = b['birthDate'];
        this.bas.$gender = b['gender'];
        this.bas.$blood_Group = b['blood_Group'];
        this.bas.$religion = b['religion'];
        this.bas.$email = b['email'];
      return this.bas;
    }

  public setBasicInformation() {
      try {
        this.basicService.getCurrentUserBasic().pipe ( map ( this.basicService.getBasicInformation ())).subscribe ( basic => {
          this.name = basic.$name;
          console.log(this.name);
          console.log('\n\n name : ' + basic['name']);
          console.log('\n\n $name : ' + basic.$name);
          this.birthDate = basic.$birthDate;
          this.gender = basic.$gender;
          console.log(basic);
        });

      } catch (e) {
        console.log('Error in Setting Basic Information!');
      }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM