How to make Http request in a angular 6 service

FrozzenFinger

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.

Suren Srapyan

You need to add @Injectable decorator on the service

import { Injectable } from '@angular/core'

@Injectable()
export class MessageService {
   ...
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Service with HTTP request in Angular

How to cancel http request in Angular 6?

how to make synchronous http request in angular js

How to make a http GET request FROM a WCF web service

How to pass data from a component to a service, to make a new http request?

Angular 6/Typescript HTTP post request returns undefined when service called from another service

How to get rid from redundant request in Angular 6 service?

How to make guard subscribes to a long polling request of a service in Angular 2

How to make a generic request service in Angular8

How to pass parameters to a $http request service in Angular 1?

How to easily mock a service HTTP request with Jasmine (Angular)?

How to test a service function in Angular that returns an $http request

How to use a Service that executes HTTP request inside an Angular Pipe

How to test a http request function in a service with Angular and Jest

Angular 6 http request handling

How to call Http post and get request in angular 6

how to make 2 dependent http request in Angular 2

How to make an HTTP request on page load in Angular (works with a button)

How to make a put request in angular2 using http?

How to make http post request in Angular 4 Universal?

How to make a sequence of http requests in Angular 6 using RxJS

Angular 6: Calling service observer.next from Http Interceptor causes infinite request loop

How to make HTTP request at an interval?

How to make an HTTP request in PHP?

How to make http request in factory

Wait for http request to complete in angular 4 service

Angular 6 http.delete request not working

Angular 6 multiple HTTP request RxJs

Angular 6 HTTP Client Post - Bad request

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive