将查询结果传递给下一个视图时发生反应,它是空的

时间:2018-05-04 21:35:04

标签: reactjs sqlite navigator

我得到了一个简单的SQLite应用程序,我试图将查询结果作为参数传递给下一个视图,但它总是为空。

我使用Button和ListView

得到了一个scrollView

            <TextInput placeholder="Marka" onChangeText={(text) => this.setState({brandSearch: text})}/>
            <TextInput placeholder="Model" onChangeText={(text) => this.setState({modelSearch: text})}/>
            <Button title="Wyszukaj"
                    color="#841584"
                onPress={() =>
                    navigate('SearchResultView', {data: this.searchCars})
                }
                     /*    onPress={this.searchCars}*/
            />

            <ListView
                enableEmptySections
                dataSource={this.state.data}
                renderSeparator={this.ListViewItemSeparator}
                renderRow={(rowData) =>
                    <View>
                        <Text style={{
                            textAlign: 'center',
                            fontWeight: "bold"
                        }}>{rowData.brand} {rowData.model} {rowData.power}km {rowData.color}</Text>

                        <Image
                            style={{width: null, height: 200}}
                            source={{uri: rowData.photos}}
                        />
                    </View>
                }
            />

        </ScrollView>

执行功能

searchCars() {
        var p = new Promise(function (resolve, reject) {
            db.transaction((tx) => {
                tx.executeSql("SELECT * FROM Car;", [], (tx, results) => {
                    var temp = [];
                    var len = results.rows.length;
                    for (let i = 0; i < len; i++) {
                        temp.push(results.rows.item(i));
                    }

                    resolve(temp);
                });
            });
        });

        p.then((result) => {
             return result;
        });
    };

1 个答案:

答案 0 :(得分:0)

您的searchCars方法正在调用p.then并在该函数调用中返回结果,但是p本身未被返回。基本上,searchCars没有返回任何内容。

function wrongSearchCars() {
  var p = new Promise(function (resolve, reject) {
        resolve('hello');
    });

    p.then((result) => {
         return result;
    });
}
function correctSearchCars() {
  var p = new Promise(function (resolve, reject) {
        resolve('hello');
    });

  return p.then((result) => {
         return result;
  });
}

wrongSearchCars().then(console.log) // undefined
correctSearchCars().then(console.log) // 'hello'

此外,正如您所看到的,由于searchCars中的.then块将返回一个promise,因此没有理由对.then进行内部调用。

searchCars() {
    return new Promise(function (resolve, reject) {
        db.transaction((tx) => {
            tx.executeSql("SELECT * FROM Car;", [], (tx, results) => {
                var temp = [];
                var len = results.rows.length;
                for (let i = 0; i < len; i++) {
                    temp.push(results.rows.item(i));
                }

                resolve(temp);
            });
        });
    });
};

根据我所看到的情况,这是我的猜测。你的代码很多都是无法推断的。