Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

230
Views
How to return data in another function from a SQLite query in React Native

i'm new to React Native, i'm trying to extrapolate a data from a query, but "return" doesn't work. Maybe because it required the promises, i tried but i'm not good enough. I call the function in the View component of the App function (in App.js) like this

<View>Search()</View>

but nothing is shown. If i put the console.log in search() (in another function) i can see the result, but i can't return it in my App.js (the function is correctly imported into the App)

export function Search() {

        db.transaction(
          (tx) => {
            tx.executeSql("select pver from profile where id=12524234", [],
            (tx, results) => {
              var a = JSON.stringify(results.rows._array[0].pver)
              var data = Number(a)
              return data;
            });
          },
          null,
        )
      };

This is how i've tried to do on advice of a user

//this in App.js
import {Search} from './Funzioni'

export default function App() {
const [searchResult, setSearchResult] = useState();


useEffect(() => {
  console.log("we are in the useeffect")
    Search().then((data)=>{
      console.log("set") //doesn't appear
        setSearchResult(data)
 })
},[]);

//other codes

return (<View>{searchResult}</View> )
}



//this in "Funzioni.js"

 export async function Search() {
        return new Promise((resolve, reject) => {
            db.transaction(
                (tx) => {
                  tx.executeSql("select pver from profile where id=12524234", [],
                  (tx, results) => {
                    var a = JSON.stringify(results.rows._array[0].pver)
                    var data = Number(a)
                    console.log("the data: " + data) //doesn't appear
                    resolve(data);
                  });
                },
                null,
              )
        });
      };
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The transaction method is asynchronous when you render your component the search function still hasn't resolved.

In your component you should use useState to store the data and useEffect to fetch the data

const [searchResult, setSearchResult] = useState('');
useEffect(() => {
    Search().then((data)=>{
        setSearchResult(data)
    }).catch((error) => console.log(error));
},[]);
return <View>{searchResult}</View>

Your Search function should be like this

export async function Search() {
    return new Promise((resolve, reject) => {
        const db = SQLite.openDatabase('THE_NAME_OF_YOUR_DATABASE.db')
        db.transaction(
            (tx) => {
              tx.executeSql("select pver from profile where id=12524234", [],
              (tx, results) => {
                var a = JSON.stringify(results.rows._array[0].pver)
                var data = Number(a)
                resolve(data);
              },
              function (error) {
                  reject(false);
                  throw new Error(
                      'Error: ' + error
                  );
              });
            },
            function (error) {
                reject(undefined);
                throw new Error('error: ' + error.message);
            },
            function () {
                console.log('ok');
            }
          )
    });
  };
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!