javascript – React Native:处理对sqllite db的异步调用
作者:互联网
我正在努力了解异步函数在React Native中的工作方式.
在这个例子中,我通过异步调用调用sqllite db调用并获取height和standard的值,并将这两个值作为一个名为result的对象返回.
sqllite db中存在值,如下面显示的控制台输出中所示.
从componentDidMount生命周期方法调用的方法是异步方法.
可以看出我正在使用等待实际执行(也就是从sqllite获取数据)来完成.
第X行始终返回未定义.
线Y似乎根本不执行,因为状态不会从100&的初始值改变. “asasd”.
我已经对代码进行了观察,我不确定我在这里缺少什么.
有人可以看看它,让我知道吗?
App.js
import React, { Component } from 'react';
import { View, Text } from 'react-native';
import {
dropLogsTable,
createLogsTable,
getProfileHeightStandardfromDB,
getProfileHeightStandardPremade,
saveLogsRecord
} from '../src/helper';
export default class App extends Component {
state = {
logarray: [],
profileobject: {profileheight: 100, profilestandard: "asasd"},
};
componentDidMount() {
dropLogsTable();
createLogsTable();
this.fetchProfileData();
}
async fetchProfileData() {
console.log('Before Profile Fetch');
const result = await getProfileHeightStandardfromDB();
console.log('After Profile Fetch');
console.log('Height : '+result.profileheight);
console.log('Standard: '+result.profilestandard);
return result; //Line X
this.setState({profileobject:result}); //Line Y
}
render() {
return (
<View>
<Text>This is a test</Text>
<Text>Profile Height : {this.state.profileobject.profileheight} </Text>
<Text>Profile Standard : {this.state.profileobject.profilestandard}</Text>
</View>
);
}
}
helper.js
import { SQLite } from 'expo';
const db = SQLite.openDatabase({ name: 'swlt.db' });
let profileheight, profilestandard;
export function getProfileHeightStandardfromDB()
{
db.transaction(
tx => {
tx.executeSql('select standard, metricweight, metricheight, imperialheight, imperialweight, bmi, metricgoalweight, imperialgoalweight from profile', [], (_, { rows }) =>
{
//console.log(rows);
console.log(rows);
//console.log(parseFloat(rows._array[0].metricheight));
profileheight = parseFloat(rows._array[0].metricheight);
profilestandard = rows._array[0].standard;
console.log('Profileheight ===>'+profileheight);
console.log('Profilestandard ===>'+profilestandard);
}
);
},
null,
null
);
const profileobject = {profileheight, profilestandard};
console.log(profileobject);
return profileobject;
}
设备和控制台的输出
解决方法:
你似乎在return语句之后有this.setState; return语句执行后没有代码.只需在返回块之前调用this.setState
此外,函数getProfileHeightStandardfromDB()需要是异步函数或需要返回Promise.目前该方法没有返回Promise,所以没有必要等待它.这就是你需要做的事情
function getProfileHeightStandardfromDB() {
return new Promise((resolve, reject) => {
db.transaction(
tx => {
tx.executeSql('select standard, metricweight, metricheight, imperialheight, imperialweight, bmi, metricgoalweight, imperialgoalweight from profile', [], (_, { rows }) => {
//console.log(rows);
console.log(rows);
//console.log(parseFloat(rows._array[0].metricheight));
profileheight = parseFloat(rows._array[0].metricheight);
profilestandard = rows._array[0].standard;
console.log('Profileheight ===>'+profileheight);
console.log('Profilestandard ===>'+profilestandard);
// what you resolve here is what will be the result of
// await getProfileHeightStandardfromDB();
resolve({ profileheight, profilestandard });
});
}, null, null);
});
}
标签:expo,javascript,reactjs,react-native,asynchronous 来源: https://codeday.me/bug/20190823/1698488.html