1

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.

0

1 Answer 1

2

Try something like this:

getTrip(): Promise{
    return new Promise((resolve, reject) => {
        const id = this.route.snapshot.paramMap.get('id');
            if (id) {
                this.tripService.getTrip(id).subscribe(
                trip => { 
                    this.trip = trip;
                    resolve();
               },
               error=>reject(error)
             );
            }
            else {
                this.trip = new Trip();
                resolve();
            }
    })
}

Usage:

this.getTrip().then(()=>{
    /*your callback here*/
})
.catch(error=>{
    /*catch exception here*/
})

If you want to use async/await, make sure that you in you compilerOptions (tsconfig.json) field "target" has "ES6" value

Sign up to request clarification or add additional context in comments.

1 Comment

@ДмитрийНикифоров Thank you. I have just noticed, however, that the json file for cities is very huge and it is not loading properly. So if I select countries with names starting with the letter >F, it is undefined because the entire file is not getting loaded. With countries having names starting with A-F, the code works fine. So I'm trying to reduce the number of cities associated with each country.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.