Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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()}
    };
}
like image 601
Rommy Avatar asked Apr 18 '17 03:04

Rommy


People also ask

What does () => void mean TypeScript?

Function Type Expressions The syntax (a: string) => void means “a function with one parameter, named a , of type string, that doesn't have a return value”. Just like with function declarations, if a parameter type isn't specified, it's implicitly any .

Is not assignable to type '() => string?

The "Type 'string' is not assignable to type" TypeScript error occurs when we try to assign a value of type string to something that expects a different type, e.g. a more specific string literal type or an enum. To solve the error use a const or a type assertion.

Is not assignable to type never []?

The error "Argument of type is not assignable to parameter of type 'never'" occurs when we declare an empty array without explicitly typing it and attempt to add elements to it. To solve the error, explicitly type the empty array, e.g. const arr: string[] = []; .

Is not assignable to parameter of type?

The error "Argument of type string | undefined is not assignable to parameter of type string" occurs when a possibly undefined value is passed to a function that expects a string . To solve the error, use a type guard to verify the value is a string before passing it to the function.


3 Answers

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')}

    );


  }

}
like image 124
user2907200 Avatar answered Oct 10 '22 13:10

user2907200


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

like image 29
Aravind Avatar answered Oct 10 '22 12:10

Aravind


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);
    }
like image 1
Julia Passynkova Avatar answered Oct 10 '22 12:10

Julia Passynkova