2

Property 'data' does not exist on type 'Object' on ng build --prod.

My service.ts

getSlogan() {
 return this.http.get(this._slogan)
}

My component.ts

import ...
.
.
export interface Iheader {
  facebook_link: string
  id: number
  instagram_link: string
  line_link: string
  pinterest_link: string
  site_slogan: string
  tel: string
  youtube_link: string
 }
 export class HeaderComponent implements OnInit {

 socials : any
 data : Iheader

 ngOnInit() {
 // get site data
  this.apiservice.getSlogan()
   .subscribe(data => {
    this.socials = data
  });

My html

<a target="_blank" [href]="socials?.data.pinterest_link">

It's error "Property 'data' does not exist on type 'Object'" on ng build --prod.

My api data look like

 [
   status: 200,
   data: {
     data: {
       pinterest_link: "url"
     }
   }
 ]
Don Noinongyao
  • 175
  • 1
  • 9

2 Answers2

1

it looks like you stored the whole server response in socials variable.

so you need to access the data like this:

<a target="_blank" [href]="socials?.data.data.pinterest_link">

Or

the better solution is to store it first like this

ngOnInit() {
   // get site data
   this.apiservice.getSlogan()
   .subscribe(response => {
      this.socials = response.data
   });
}

and you need to set the socials variable to something by default like empty array.

socials : any = []
Mahmoud Fawzy
  • 1,635
  • 11
  • 22
1

socials?.data.pinterest_link should be the error. "socials?" just checks, if its undefined/null or defined. But if it's defined, it does not guarantee, that the property "data" exist on the object. You need something like "socials?.data?", but the href will have errors.

Furthermore, you have a double nested "data". Maybe this is also an error.

Do it like

 <a target="_blank" [href]="socials && socials.data ? socials.data.pinterest_link : ''">
Develobba
  • 716
  • 8
  • 22