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()}
};
}
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 .
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.
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[] = []; .
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.
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')}
);
}
}
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
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With