我如何在React Native中获得函数的结果

时间:2019-07-17 16:58:40

标签: reactjs react-native

我没有得到数据库查询的结果,而是得到了'undefined'

有人可以告诉我这里缺少什么吗?

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

1 个答案:

答案 0 :(得分:1)

您需要返回一个Promiseresolve及其结果。

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