0

this.getDetails();

getDetails = () => {
     let userDetails = [];
     db.transaction((tx) => {
          tx.executeSql('select * from users where role=?',['students'],(tx,results) => {
               let details = []
               if(results.rows.length > 0){
                    for(let i=0; i < results.rows.length; i++){
                          details.push(results.rows.item(i));
                    }
               }
               userDetails = details;
               //Data is displayed  here
               console.log(userDetails)
          })
     })
     // Data is not printed here
     console.log(userDetails) // []
}

In the above code snippet , I am able to get the data(userDetails) inside the tx.executeSql but not outside the db.transaction block.

Can anyone please help me out how to display the data outside of the db.transaction block.

1 Answer 1

1

Define a promise function for getting userDetails from DB.

async dbAction = () => {
  return new Promise((resolve, reject) => {
    db.transaction((tx) => {
          tx.executeSql('select * from users where role=?',['students'],(tx,results) => {
               let details = []
               if(results.rows.length > 0){
                    for(let i=0; i < results.rows.length; i++){
                          details.push(results.rows.item(i));
                    }
               }
               //Data is displayed  here
               resolve(details);
          })
     })
  });
}

And then you can call it like this.

getDetails = () => {
     let userDetails = [];
     try {
       let userDetails = await dbAction();
       console.log(userDetails);
     } catch (e) {
     }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hey, thanks a lot. You saved my day and minimized my effort. Thanks once again.

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.