Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

232
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda