Home > Mobile >  Getting some issues using {return this.getAll.get(this.url)} when consuming an API in Angular
Getting some issues using {return this.getAll.get(this.url)} when consuming an API in Angular

Time:04-27

I am trying to consume an API using Angular and Typescript, my previous knowledge in using API's is mainly in Javascript so I am pretty new to it, however what I have so far is giving me an issue:

I created a new service file:

growth-service.ts

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class GrowthService {

  url = 'https://uks-tst-tbp-gw.azurewebsites.net/business/getcategories'
  constructor(private http:HttpClient) { }

  getAllOptions(): any{
    return this.getAllOptions.get(this.url)
  }
}

but the line return this.getAllOptions.get(this.url) is showing me an error on .get saying that

TS2339: Property 'get' does not exist on type

However I cannot find a reason as to why?

CodePudding user response:

You are calling getAllOptions from the function getAllOptions()itself

probably what you want to do is:

getAllOptions(): Observable<any>{
  return this.http.get(this.url)
}

also as a naming convention you should call function that returns an Observable with a dollar sign at the end i.e. getAllOptions$(){}

  • Related