无法读取未定义类型错误的 属性 'subscribe'
Cannot read property 'subscribe' of undefined TypeError
我正在开发一个 Ionic 应用程序,我有以下调用可观察对象的方法:
getCountryById(id: number): Promise<Country> {
return new Promise((resolve, reject) => {
this.getCountries().subscribe(countries => {
for (let country of countries) {
if (country.Id == id) {
resolve(country);
}
}
resolve(undefined);
}, err => reject(err));
})
}
另一种方法:
getCountries(): Observable<Country[]> {
if (this.countries) {
return Observable.of(this.countries);
} else if (this.countriesObservable) {
return this.countriesObservable;
} else {
this.storage.get(AppSettings.STORAGE_KEYS.LANGUAGE_APP).then(
language=>{
this.countriesObservable = this.http.get(AppSettings.API_URL + 'api/Countries?locale=' + language).map(json => {
delete this.countriesObservable; // when the cached countries is available we don't need the `countriesObservable` reference anymore
this.countries = json as Country[];
this.countries = this.countries.sort(function (a, b) { return (a.Name > b.Name) ? 1 : ((b.Name > a.Name) ? -1 : 0); });
return this.countries;
}).share();
}
).catch(err=>{
});
return this.countriesObservable;
}
}
我很确定我 return输入了错误的数据。我应该如何将第二种方法重构为 return 有效的 Observable,以便第一种方法可以在 this.I 上工作我仍在尝试围绕 Promise 和 Observable。感谢您的帮助。
你的问题是,当 this.countriesObservable
是 undefined
时,你正在调用 this.storage.get(...).then(...)
。在你设置 this.countriesObservable
.
的 promise 回调中
问题是,当你到达 return this.countriesObservable
时,then
的回调还没有执行,所以你还在 returning undefined
.
你必须在调用 this.storage.get
(可能是 Subject
)之前将 this.countriesObservable
分配给一个新的 Observable
,然后,在 then
中,你只需收听您要 return 的 Observable,并在其对 subscribe
的调用中,向 this.countriesObservable
提供您想要的数据:
const _subject = new Subject<Country[]>();
this.countriesObservable = _subject;
this.storage.get(AppSettings.STORAGE_KEYS.LANGUAGE_APP).then(
language=>{
this.http.get(AppSettings.API_URL + 'api/Countries?locale=' + language).map(json => {
delete this.countriesObservable; // when the cached countries is available we don't need the `countriesObservable` reference anymore
this.countries = json as Country[];
this.countries = this.countries.sort(function (a, b) { return (a.Name > b.Name) ? 1 : ((b.Name > a.Name) ? -1 : 0); });
return this.countries;
}).subscribe(countries => _subject.next(countries));
}
).catch(err=>{});
return this.countriesObservable;
您可能需要进行一些调整,但这就是我的想法。希望对你有用。
我正在开发一个 Ionic 应用程序,我有以下调用可观察对象的方法:
getCountryById(id: number): Promise<Country> {
return new Promise((resolve, reject) => {
this.getCountries().subscribe(countries => {
for (let country of countries) {
if (country.Id == id) {
resolve(country);
}
}
resolve(undefined);
}, err => reject(err));
})
}
另一种方法:
getCountries(): Observable<Country[]> {
if (this.countries) {
return Observable.of(this.countries);
} else if (this.countriesObservable) {
return this.countriesObservable;
} else {
this.storage.get(AppSettings.STORAGE_KEYS.LANGUAGE_APP).then(
language=>{
this.countriesObservable = this.http.get(AppSettings.API_URL + 'api/Countries?locale=' + language).map(json => {
delete this.countriesObservable; // when the cached countries is available we don't need the `countriesObservable` reference anymore
this.countries = json as Country[];
this.countries = this.countries.sort(function (a, b) { return (a.Name > b.Name) ? 1 : ((b.Name > a.Name) ? -1 : 0); });
return this.countries;
}).share();
}
).catch(err=>{
});
return this.countriesObservable;
}
}
我很确定我 return输入了错误的数据。我应该如何将第二种方法重构为 return 有效的 Observable,以便第一种方法可以在 this.I 上工作我仍在尝试围绕 Promise 和 Observable。感谢您的帮助。
你的问题是,当 this.countriesObservable
是 undefined
时,你正在调用 this.storage.get(...).then(...)
。在你设置 this.countriesObservable
.
问题是,当你到达 return this.countriesObservable
时,then
的回调还没有执行,所以你还在 returning undefined
.
你必须在调用 this.storage.get
(可能是 Subject
)之前将 this.countriesObservable
分配给一个新的 Observable
,然后,在 then
中,你只需收听您要 return 的 Observable,并在其对 subscribe
的调用中,向 this.countriesObservable
提供您想要的数据:
const _subject = new Subject<Country[]>();
this.countriesObservable = _subject;
this.storage.get(AppSettings.STORAGE_KEYS.LANGUAGE_APP).then(
language=>{
this.http.get(AppSettings.API_URL + 'api/Countries?locale=' + language).map(json => {
delete this.countriesObservable; // when the cached countries is available we don't need the `countriesObservable` reference anymore
this.countries = json as Country[];
this.countries = this.countries.sort(function (a, b) { return (a.Name > b.Name) ? 1 : ((b.Name > a.Name) ? -1 : 0); });
return this.countries;
}).subscribe(countries => _subject.next(countries));
}
).catch(err=>{});
return this.countriesObservable;
您可能需要进行一些调整,但这就是我的想法。希望对你有用。