Despite reading various similar questions, I'm still struggling to make the .find()
function work in my application.
I have a service with the following API:
export class VehicleService {
private defUrl = 'API';
constructor(private http: Http) { }
getVehicleByVehicleId(vehicleid?: any) {
const url = (!vehicleid) ? this.defUrl : 'API1&vehicle_id=' + vehicleid;
return this.http.get(url)
.map(res => res.json());
}
searchVehicleId(description?: string) {
const url = (!description) ? this.defUrl : 'API2&description=' + description;
return this.http.get(url)
.map(res => res.json().vehicles);
}
}
Component
export class VehicleComponent implements OnInit {
ngOnInit() {
this.searchQuery = new FormGroup({
vehicleDescription: new FormControl('', Validators.required)
});
}
constructor(private vehicleService: VehicleService) { }
public fleet: Fleet[];
public description: Description[];
public searchQuery: FormGroup;
public searchVehicleId: any;
public searchByVehicleId(searchQuery) {
this.vehicleService
.searchVehicleId(searchQuery.value.vehicleDescription)
.subscribe(searchQuery => {
this.description = searchQuery;
this.searchVehicleId = this.description.find() // <= HOW TO IMPLEMENT THIS?
this.vehicleService
.getVehicleByVehicleId(this.searchVehicleId)
.subscribe(searchQuery => {
this.vehicle = searchQuery;
})
});
}
interface Vehicle {
status: number;
dallases: Vehicle[];
}
interface VehicleDetails {
vehicle_id: number;
dallassettings: string;
dallasupdated: string;
dallas_list: DallasList[];
}
interface DallasList {
number: number;
auth: number;
}
//////////////////////////////////
//////////////////////////////////
interface Description {
vehicles: DescriptionDetails[];
}
interface DescriptionDetails {
id: number;
custom_description: string;
description: string;
}
API2
{
"vehicles": [{
"description": "SKODA ROOMSTER",
"id": 123456,
"custom_description": ""
}, {
"description": "SKODA ROOMSTER",
"id": 123456789,
"custom_description": ""
}]
}
My approach involves passing the description
from the FormGroup
- specifically the vehicleDescription
field - to the searchByVehicleId
method. The goal is to find the id
corresponding to the provided description
value (from the 'description' field in API2). Once we locate the id
, it is used further in
this.vehicleService.getVehicleByVehicleId
...
The main challenge lies in my lack of understanding on how to correctly implement the .find()
method.
Thank you