I created a service and tried to call a method in that service. When I try to call a method in the service, I get the following error.
this.searchService.getQueryResults is not a function
The method is being called in a component function to handle clicking a button on the form. It is like the service is never being instantiated when it is injected into the components file. The service doesn't do much at the moment. It should just return an array of data defined in an interface.
This is the service:
import { Injectable } from '@angular/core';
import {Advsearchresult} from "../../models/search/advsearchresult";
@Injectable({
providedIn: 'root',
})
export class Searchservice {
private searchResults: Array = [];
getQueryResults():Advsearchresult[] {
this.searchResults = [{..},{...}];
return this.searchResults;
}
This is the interface being used:
export interface Advsearchresult {
documentType: string;
assetClass: string;
documentTitle: string;
documentNumber: string;
revisionNumber: string;
revisionDate: number;
documentState: string;
documentStatus: string;
checkedOut: boolean;
}
This is the onclick calling the service:
doSubmit(){
console.log("searchresults-fake",JSON.stringify(this.searchService.getQueryResults()));
}
The service is being injected into the components.ts:
private searchService: Searchservice = Inject(Searchservice);
What have I done wrong?
The app is using Ionic, in case that matters.