I have a strange problem: I have a simple function to get the user information from my DB in myData.ts.
test = <any>{};
getUserInfoFromPouch(filter: string):any{
this.db.get(filter).then((result) => {
this.test = result;
})
return this.test;
}
and it works ... kinda.
I call the function in my user-data-page constructor:
this.userData = this.myData.getUserInfoFromPouch(this.userId);
console.log('------------------t1----------------------------------');
console.log(this.userData);
console.log('------------------t2----------------------------------');
console.log(this.myData.test);
but I only get an ampty object back:
------------------t1---------------------------------- main.js:62736:9
Object { } main.js:62737:9
------------------t2---------------------------------- main.js:62738:9
Object { }
If I call the function again via a button, I get the actual output I would expect. If I don't call the function in the constructor first, I have to use the button twice to get the data. That is very confusing. I tried many different function spellings like:
test = <any>{};
getUserInfoFromPouch(filter: string):any{
return this.db.get(filter).then((result) => {
})
}
or
test = <any>{};
getUserInfoFromPouch(filter: string):any{
this.db.get(filter).then((result) => {
return result;
})
}
What am I doing wrong? Thank you for help.
Btw. I Have only one record in the database. (for test purposes)
Edit: Okay, I tried the function like this:
async getUserInfoFromPouch(_id: string){
try {
this.test = await this.db.get(_id);
} catch (err) {
console.log(err);
}
console.log('------------------t3----------------------------------');
console.log(this.test);
}
and I call the function in the constructor of my user page, like before. After I called the function, I fill my userdata with the test var from the function:
this.myData.getUserInfoFromPouch("userinfo");
this.userData = this.myData.test;
console.log('------------------t1----------------------------------');
console.log(this.userData);
but the userData is empty. And I get the console.output of the function AFTER I did:
this.userData = this.myData.test;
I'm doing something pretty wrong ;-(