Search code examples
angularangular-providers

errors.ts:42 ERROR Error: Uncaught (in promise):


Am having this error on my chatservice i can't solve it
Here's the error

here is the actual code of my chatservice

import{Injectable} from '@angular/core';
import {Subject} from 'rxjs/Subject';
import {Observable} from 'rxjs/Observable';
 import * as io from 'socket.io-client';
export class chatService{
    private url = 'http://localhost:8000'
    private socket:any;

    sendMessage(message:string){
        this.socket.emit('add-message', message);
    }
    getMessages(){
        let observable = new Observable(( observer:any)=>{
            this.socket = io(this.url);
            this.socket.on('message',(data:any)=>{
                observer.next(data);
            });
            return () => {
                this.socket.disconnect();
            }
        })
        return observable;
    }

}  

Solution

    1. Firstly, as per the Angular Style Guide, you should use a capital letter for your service (aka. chatService => ChatService)
    2. Next, did you include ChatService as a provider in your application's module file?

      @NgModule({
          providers: [
              ChatService
          ]
      })