I have a Trip object:
{ "id": "1", "country":"UK", "city": "London" }
I'm trying to create an edit form, where the form gets populated with the values from the trip object. I'm able to populate the countries dropdown (in ngOnInit()), and the selected country has a binding to trip.country. But I'm not able to fetch the cities of that country, because apparently the trip is still undefined when it is supposed to get trip.country to find the cities of that country. (I try to print the trip, and get undefined)
I tried using async with the getTrip() function, but I get the error that:
Error:(71, 21) TS1055:Type 'void' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value.
I think the solution is some kind of async/await/promise strategy but I can't get hold of it.
export class TripEditorComponent implements OnInit {
trip;
countries;
cities;
ngOnInit() {
this.getTrip();
this.countries = this.countryService.getCountries().subscribe(
data => { this.countries = data;},
err => console.error(err),
() => console.log('done loading countries')
);
console.log("this trip: ", this.trip) //this is undefined
this.cities = this.cityService.getCitiesByCountry(this.trip.country).subscribe(
data => { this.cities = data; },
err => console.error(err),
() => console.log('done loading cities') );
}
getTrip(): void {
const id = this.route.snapshot.paramMap.get('id');
if (id) {
this.tripService.getTrip(id).subscribe(trip => { this.trip = trip; });
} else {
this.trip = new Trip();
}
}
}
Any help will be appreciated.