Helmut
Helmut

Reputation: 43

How can i get the result of a function in React Native

Im not getting the result of a database query, instead im getting 'undefined'

Can someone pls tell me what im missing here?

DbManager.js

getCameras() {
    db.transaction(tx => {
        tx.executeSql('select * from cameras;', [],
            (_, result) => { return result.rows._array });
    });
}

App.Js

async componentDidMount() {
    dbManager.createDatabase();
    //dbManager.insertCamera('Canon Canonet QL 17');
    let data = await dbManager.getCameras();
    console.log(data);
}

Upvotes: 2

Views: 73

Answers (1)

Vencovsky
Vencovsky

Reputation: 31655

You need to return a Promise and resolve with the result.

getCameras() {
    return new Promise((resolve, reject) => {
        db.transaction(tx => {
            tx.executeSql('select * from cameras;', [], (_, result) => { 
                resolve(result.rows._array) 
            });
        });
    })
}


async componentDidMount() {
    dbManager.createDatabase();
    //dbManager.insertCamera('Canon Canonet QL 17');
    let data = await dbManager.getCameras();
    console.log(data);
}

Upvotes: 1

Related Questions