Service Injection to React Component (is missing type“nameofmyinterface” []: length, pop, push, concat, and 16 more (SPFx))
-
17-02-2021 - |
Pregunta
I have this service that gets all the list items from a Sharepoint List (Promise)
public getVehicles(): Promise<IVehiclesListItem[]> {
let promise: Promise<IVehiclesListItem[]> = new Promise<IVehiclesListItem[]>((
resolve,
reject
) => {
let query = `${this.siteAbsoluteUrl}${LIST_API_ENDPOINT}/items?${SELECT_QUERY}`;
this.client.get(
query,
SPHttpClient.configurations.v1,
this._spHttpOptions.getNoMetadata
)
.then((response: SPHttpClientResponse): Promise<{value: IVehiclesListItem[] }> => {
return response.json();
})
.then((response: {value: IVehiclesListItem[] }) => {
resolve(response.value);
})
.catch((error: any) =>{
reject(error);
});
});
return promise;
}
My interface:
export interface IVehiclesListItem {
['@odata.type']?: string; //? (not required) for create or update
['@odata.etag']?: string;
Id: number;
Title?: string;
Data?: string;
State?: string;
}
Now i'm trying to call that service on my React component like this:
public constructor(props:IVehiclesReactProps,any)
{
super(props);
this.state={
vehicles:[]
};
}
public componentDidMount(): void
{
let vehiclesInjection = this.vehiclesService.getVehicles();
this.setState({
vehicles: vehiclesInjection
});
}
But i get the following error
Type 'Promise<VehiclesListItem[]>' is missing the following properties from type 'VehiclesListItem[]': length, pop, push, concat, and 16 more.
No hay solución correcta
Licenciado bajo: CC-BY-SA con atribución
No afiliado a sharepoint.stackexchange