Rommy
Rommy

Reputation: 447

type void is not assignable to type any

I have a code in my component

export class AddNewCardComponent { 

    public concept = [];
    constructor( 
        private _router: Router,
        private _empDiscService: empDiscService) { 

        }

    ngOnInit(){
        this.concept = this._empDiscService.StoreMaster()
        .subscribe((data)=>{
            this.concept =  data;
        });
    }
}

its just i want to get var from services and put it in my store = [];

but it says

type void is not assignable to type any

here is my code in services

import {Injectable} from '@angular/core';
import { Httpprovider } from './httpprovider';

@Injectable()
export class empDiscService{

    public storedata = [];
    constructor(private _httpprovider: Httpprovider){

    }

    StoreMaster():Observable<any[]>{
        return this._httpprovider.httpReq('http://192.168.1.40:5000/getconcept','GET',null,null).map(res =>{<any[]>res.json()}
    };
}

Upvotes: 6

Views: 45542

Answers (3)

Julia Passynkova
Julia Passynkova

Reputation: 17899

StoreMaster() method does not return anything. You might need to change the service method to return the observable:

StoreMaster(){
return this._httpprovider.httpReq('http://192.168.1.40:5000/getmapmasterstore','' +
  '  GET',null,null).subscribe((data)=>{
  var brData = [];
  for (let i=0;i<data.length;i++){
    brData.push(data[i]);
  }
  return brData;
});
}

and ngOnInit

    ngOnInit(){
        this._empDiscService.StoreMaster()
            .subscribe(store => this.store = store);
    }

Upvotes: 1

user2907200
user2907200

Reputation: 31

import { JobService } from './../services/job.service';
import { Component, OnInit } from '@angular/core';



@Component({
  selector: 'app-job-list',
  templateUrl: './job-list.component.html',
  styleUrls: ['./job-list.component.css']
})
export class JobListComponent implements OnInit {

 **jobs:any = [];**
erreur = '';
  constructor( private jobservice:JobService) { }

  ngOnInit() {
    this.jobservice.getJobs().subscribe(

      data => this.jobs = data,
      error => {console.log ('error')}

    );


  }

}

Upvotes: 3

Aravind
Aravind

Reputation: 41581

You shall not subscribe() inside the service and return the object directly, which is not the intention of RxJs.

export class empDiscService{

    public storedata = [];
    constructor(private _httpprovider: Httpprovider){

    }
    StoreMaster():Observable<any[]>{
       return this._httpprovider.httpReq('http://192.168.1.40:5000/getmapmasterstore','GET',null,null).map(res =>{<any[]>res.json()}
    }

}

Your component should be

constructor( 
        private _router: Router,
        private _empDiscService: empDiscService) { 
          this.store = this._empDiscService.StoreMaster()
            .subscribe((data)=>{
                    this.storedata =  data;
        });
}

The actual mistake which you made is the service method was not returning anything

StoreMaster():Observable<any[]>{}

This will fix your problem

Upvotes: 1

Related Questions