Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
621 views
in Technique[技术] by (71.8m points)

typescript - Error in angular: Cannot read property 'Name' of undefined

Last time when i tried to do subscriptions and mapping in angular was 2 years ago. Im tired of it, i trying for week or more to map that values and the closest attempt getting me error: Cannot read property 'Name' of undefined.

This is my code:

this.languageService.getAllLanguages().subscribe(languages =>{
      this.languages = languages.map((language)=>{
        return {
          Id:language.Id, 
          Name: language.Name,
          Code: language.Code,
          Charset: language.Charset,
          Default: language.Default,
          Active: language.Active,
          SystemId: language.SystemId
        }
      })
    })



    this.languageTableElements = [
      {
        type: 'checkbox'
      },
      {
        type: 'icon',
        label: this.languages[0].Name,
        icon: 'assets/icons/flag_pl.png'
      }
    ]
question from:https://stackoverflow.com/questions/65915920/error-in-angular-cannot-read-property-name-of-undefined

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Need to use languages in the subscription callback (when it has a value!)

this.languageService.getAllLanguages().subscribe(languages =>{
  this.languages = languages.map((language)=>{
    return {
      Id:language.Id, 
      Name: language.Name,
      Code: language.Code,
      Charset: language.Charset,
      Default: language.Default,
      Active: language.Active,
      SystemId: language.SystemId
    }
  })

  this.languageTableElements = [
    {
      type: 'checkbox'
    },
    {
      type: 'icon',
      label: this.languages[0].Name,
      icon: 'assets/icons/flag_pl.png'
    }
  ]
})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...