Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Http request in a angular 6 service

here is my thing, I have a message service which I want to gather data from my server through HTTP requests (Rest Api) like this:

import { Sensor } from './sensor.model'
import { HttpClient } from '@angular/common/http';

export class MessageService{
  mqttMessageData : JSON[]=[];
  coapMessageData : JSON[]=[];
  xmppMessageData : JSON[]=[];

  constructor(private httpClient: HttpClient) {

  }
getMqttMessages() {
    this.httpClient.get<JSON>('http://127.0.0.1:5002/messages/mqtt').subscribe(data => {
      this.mqttMessageData.push(data);
      console.log(data);
    });
  }

but i have this error on my browser console (no errors in ng serve) :

Uncaught Error: Can't resolve all parameters for MessageService: (?).
    at syntaxError (compiler.js:1016)
    at CompileMetadataResolver.push../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver._getDependenciesMetadata (compiler.js:10917)
    at CompileMetadataResolver.push../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver._getTypeMetadata (compiler.js:10810)
    at CompileMetadataResolver.push../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver._getInjectableTypeMetadata (compiler.js:11032)
    at CompileMetadataResolver.push../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver.getProviderMetadata (compiler.js:11041)
    at compiler.js:10979
    at Array.forEach (<anonymous>)
    at CompileMetadataResolver.push../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver._getProvidersMetadata (compiler.js:10939)
    at CompileMetadataResolver.push../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver.getNonNormalizedDirectiveMetadata (compiler.js:10416)
    at CompileMetadataResolver.push../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver._getEntryComponentMetadata (compiler.js:11013)

so I assume there is an issue with the httpClient but i don't know how to do this in an other way.

like image 920
FrozzenFinger Avatar asked Dec 29 '25 23:12

FrozzenFinger


1 Answers

You need to add @Injectable decorator on the service

import { Injectable } from '@angular/core'

@Injectable()
export class MessageService {
   ...
}
like image 51
Suren Srapyan Avatar answered Jan 01 '26 15:01

Suren Srapyan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!