I'm trying to get some information from the database through the get()
function, but when I try to run it gets the error of the title, stating
at HomeTabPage.push ../src/app/home-tab/home-tab.page.ts.HomeTabPage.ngOnInit (home-tab.page.ts: 18)
No *ngFor I already tried to use the Elvis parameter, but it did not work. I already passed my main function out of ngOnInit and declared another function to reference, but it did not work.
home-tab.page.html
<ion-list >
<ion-item *ngFor="let advert of advertList">
<ion-thumbnail slot="start">
<ion-img src="advert?.image"></ion-img>
</ion-thumbnail>
<ion-label>
<h2 >{{advert?.name}}</h2>
<h3 >{{advert?.price}}</h3>
<p >{{advert?.description}}</p>
</ion-label>
</ion-item>
</ion-list>
home-tab.page.ts
export class HomeTabPage implements OnInit {
public advertList: Array<any>;
constructor(private navCtrl: NavController, public adService: AdvertisingService) {}
ngOnInit(){
this.adService.getAdAllList().get().then(advertListSnapshot => {
this.advertList = [];
advertListSnapshot.forEach(snap =>{
this.advertList.push({
id: snap.id,
name: snap.data().name,
price: snap.data().price,
description: snap.data().description,
image: snap.data().picture
});
});
});
}
}
service.ts
export class AdvertisingService {
public adAllListRef: firebase.firestore.CollectionReference;
constructor() {
firebase.auth().onAuthStateChanged(user => {
if (user) {
this.adAllListRef = firebase.firestore().collection(`/adverts/adAll/adList`);
}
});
}
//cRud -> Read
getAdAllList(): firebase.firestore.CollectionReference {
return this.adAllListRef;
}
}
ERROR
HomeTabPage_Host.ngfactory.js? [sm]:1 ERROR TypeError: Cannot read property 'get' of undefined
at HomeTabPage.push../src/app/home-tab/home-tab.page.ts.HomeTabPage.ngOnInit (home-tab.page.ts:18)
at checkAndUpdateDirectiveInline (core.js:22099)
at checkAndUpdateNodeInline (core.js:23363)
at checkAndUpdateNode (core.js:23325)
at debugCheckAndUpdateNode (core.js:23959)
at debugCheckDirectivesFn (core.js:23919)
at Object.eval [as updateDirectives] (HomeTabPage_Host.ngfactory.js? [sm]:1)
at Object.debugUpdateDirectives [as updateDirectives] (core.js:23911)
at checkAndUpdateView (core.js:23307)
at callViewAction (core.js:23548)
this.adService.getAdAllList().get().then(..
it looks like the `getAdAllList() is returning null and therefore calling get on an undefined value throws the error. – Kalenda