1

I am currently developing a mobile app using ionic 2. i have this set of code that need to retrieve data from sqlite and then further take this value for some other functionality. See below

this.credentials = _credentials;
let username = _credentials.username;
let password = _credentials.password;
let cVersion;
**this.localDbService.getcVersion().then((data) => {
    cVersion = data;
});**
console.log(cVersion);
//build http header and make http call to servlet to retrieve details
let myHeaders: Headers = new Headers();
let digest = btoa(`${username}:${password}`);

The value of cVersion is null since I am guessing that the call to sqlite is an asynchronous one. One quick fix which I think would work will be to include all the next logic in the database call function getcVersion but is there any solution like waiting for the database call to end and then continue executing after?

Thanks, Ashley

1
  • I think that is the whole purpose of .then() function. You can shift all the code you have which needs cVersion, in it's .then() part. Also, make sure to take necessary actions in .catch(). Commented Mar 20, 2017 at 9:37

1 Answer 1

2
Replace this

this.credentials = _credentials; 
let username = _credentials.username; 
let password = _credentials.password; 
let cVersion;

 this.localDbService.getcVersion()
.then((data) => { 

cVersion = data;

console.log(cVersion);

//build http header and make http call to servlet to retrieve details 
let myHeaders: Headers = new Headers(); 
let digest = btoa(`${username}:${password}`);

 });

Cheers!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.