Home > database >  How to return data in another function from a SQLite query in React Native
How to return data in another function from a SQLite query in React Native

Time:01-20

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,
              )
        });
      };

CodePudding user response:

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');
            }
          )
    });
  };
  •  Tags:  
  • Related