2

I have to get data from flickr API. Precisely I need "photo" array from my URL. It works when I getting to data in app.component and operate on any, but I know that this is not a good solution.

I Tried:

photo.ts:

  export class Photo {
    title: string;
    farm: number;
    secret: string;
    server: string;
    owner: string;
    id: string;
  };

app.component.ts:

  export class AppComponent implements OnInit {

    photos: Photo[];

    constructor(private http: HttpService) {}

    ngOnInit() {
      this.getData();
    }

    getData() {
      this.http.getData().subscribe(data => {
        this.photos = data;
      });
    }
  }

and my main problem, http.service.ts:

export class HttpService {

  constructor(private http: HttpClient) { }

  getData(): Observable<Photo[]> {
    return this.http.get('https://api.flickr.com/path')
      .map(res => res.photos.photo);
  }
}

It seems to me that everything should work fine, but I get an error:

ERROR in src/app/http.service.ts(18,23): error TS2339: Property 'photos' does not exist on type 'Object'.
8
  • 3
    what's the result of photos or res. did you check ? Commented Nov 19, 2018 at 22:07
  • tap before map to verify the result. Commented Nov 19, 2018 at 22:11
  • @Manjunath i edit the question and add console.log(res) img. Commented Nov 19, 2018 at 22:22
  • But wait, is it a runtime error or compile time errror? Commented Nov 19, 2018 at 22:24
  • its a compile time error Commented Nov 19, 2018 at 22:25

3 Answers 3

6

Try implicitly setting res to 'any'

getData(): Observable<Photo[]> {
  return this.http.get('https://api.flickr.com/path')
    .map((res:any) => res.photos.photo);
}
Sign up to request clarification or add additional context in comments.

Comments

1

Try this instead in your photo.ts to match the response structure

export interface Photo {
    title: string,
    farm: number,
    secret: string,
    server: string,
    owner: string,
    id: string,
    isFriend: number,
    isFamily: number,
    isPublic: number
  };

And in your callback change to this.photos = data.photos.photo

2 Comments

unfortunately it didn't help.
You need to change aswell in your callback to this.photos = data.photos.photo
-2

This is very useful video with good example to fetch data from api using angularjs

https://www.youtube.com/watch?v=Lp8d4VZyhZc&t=666s

2 Comments

Hi, AngularJS is not the same as Angular (or Angular 2+)
They are very different. AnajuarJS can be embedded in any HTML code, cshtml, php, etc. But Angular mainly used as stand-alone frontend application.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.