-1

I have a service with this code:

cars() {
    return 'something here';
}

Then from the component I want to get the data with an observable so I'm doing this:

getcars() {
    this.dataService.cars().subscribe((result) => {
      console.log(result);
  });
}

I cannot do this as I'm getting:

Error: Property 'subscribe' does not exist on type 'string'.

How can I fix this?

joe2020wow
  • 237
  • 5
  • 12
  • What will 'something here' be..? Just a string? Or a http call to an external service? – MikeOne Jun 16 '20 at 20:02
  • `subscribe` keyword is used to attach to a RxJS [observable](https://rxjs-dev.firebaseapp.com/guide/observable) and listen to it's notifications. It does not exist on any other types. – ruth Jun 16 '20 at 20:06

1 Answers1

1

The error message: Property 'subscribe' does not exist on type 'string'. Is telling you that you are trying to call the subscribe() function on a string. Strings aren't an Observable that you can subscribe to. You should either not use subscribe or return an observable from the service.

To create an observable from a value, you can use of.

import { of } from 'rxjs';

cars() {
  return of('something here');
}
Chris Danna
  • 1,164
  • 8
  • 17