0

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 ;-(

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
user3793935
  • 411
  • 5
  • 22
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Saravana May 26 '17 at 09:34
  • mhm I tried a async aproach, but it still doesn't work. If I call the function, I get the expected data, but for some reasons, I get the data ( if I call it in this order): this.myData.getUserInfoFromPouch(this.userId); this.userData = this.myData.test; console.log('------------------t1----------------------------------'); console.log(this.userData); after the console.log(this.userData); – user3793935 May 26 '17 at 11:23

1 Answers1

1

Okay, it solved the problem with promises.

 this.myData.getDocumentById('userInfo').then(res=>{
      if (res!=undefined){
        this.userData=res;
      } 
    }).catch(err=>{
      console.log('userInfo-error:  ', err)
    }); 

Now it works as it should my getDocumentById function locks like this:

 getDocumentById(id: string):any {
    this.syncTwoWay();
    return this.db.get(id).then((doc) => {
      return doc;
    }).catch((err) => {
      console.log('getDocumentById' + err);
    });
  }
user3793935
  • 411
  • 5
  • 22