A function whose declared type is neither void nor any should return a value

In my angular2 application, I have a service that sends a request for a url.

Here is my service:

    import {Http} from '@angular/http';
    import {Observable} from 'rxjs/Observable';
    import 'rxjs/add/operator/map';
    import {Injectable} from '@angular/core';
    import {Gallery} from './gallery';

    @Injectable()
    export class InstagramService{

        constructor(private _http:Http){

        }

        getGallery(username: string) : Observable<Gallery> {
            var result=this._http.get("https://www.instagram.com/"+username+"/media/").map(res => res.json());
 console.log(result);
        return result;
        }

    }

I defined the return type as Observable<Gallery>, but he complains that:

A function whose declared type is neither 'void' nor 'any' must return a value

What happens with this code?

+4
source share
1 answer

If you declare a return type, then why not return anything?

This is usually what you want in this case.

getGallery(username: string) : Observable<Gallery> {
     return this._http.get("https://www.instagram.com/"+username+"/media/").map(res => res.json());
}

var resultprobably not the way you expect, because it _http.get(...)returns Observableno value.

Then the caller getGallery()can sign up to receive a notification when a value arrives.

instagramService.getGallery.subscribe((result) => this.galleryData = result);

, (result) => this.galleryData = result, result galleryData .

+4

Source: https://habr.com/ru/post/1650587/


All Articles