I'm trying to get two objects from firebase and manipulate the data, before it's displayed. Unfortunately I do only get one of the objects but I don't know why.
Here are the code snippets:
page.ts
let item: ItemModel = this.ItemProvider.getItem(itemID);
let itemInfo: ItemInfoModel = this.ItemProvider.getItemInfo(itemID);
provider.ts
item: ItemModel = new ItemModel();
itemInfo: ItemInfoModel = new ItemInfoModel();
getItem(itemID: string): ItemModel {
var subscription = this.db.object(`/items/${itemID}`, { preserveSnapshot: true })
.subscribe(item => {
let i: ItemModel = item.val();
this.item = i;
});
subscription.unsubscribe();
return this.item;
}
getItemInfo(itemID: string): ItemInfoModel {
var subscription = this.db.object(`/itemInfo/${itemID}`, { preserveSnapshot: true })
.subscribe(itemInfo => {
let i: ItemInfoModel = itemInfo.val();
this.itemInfo = i;
});
subscription.unsubscribe();
return this.itemInfo;
}
As you can see both functions are similar but still the getItemInfo doesn't return the value in time.
When I do console.log in page.ts in prints the following
console.log(item); => Object { data... }
console.log(itemInfo); => ItemInfoModel { }
I tried to switch the calls of the functions and get itemInfo first, but it doesn't have any effect.
The structure of my firebase is like this:
- firebase
- items
-KIedkI3983ndK
-name
-img
-price
-actualInfo => KIe3jkdl83dsf
- itemInfo
-KIeDjeidl3njd
-item => KIedkI3983ndK
-info1
-info2
-KIe3jkdl83dsf
-item => KIedkI3983ndK
-info1
-info2
Since I'm still pretty new to this I tried everything I've read here on stackoverflow but didn't find a solution for the problem.
In the subscription of getItemInfo I do have the right value, but it's not written in the global this.itemInfo before the return-statement is called. It's wired that in getItem the exact same code works.
Any ideas?