Und*_*Dog 5 javascript asynchronous reactjs react-native expo
我在尝试了解async函数在React Native中的工作方式时有些挣扎。
在此示例中,我通过异步调用来调用sqllite db调用,并获取和的值,height并将standard这两个值作为称为的对象返回result。
如下面的控制台输出所示,值在sqllite db中存在。
从componentDidMount生命周期方法调用的方法是异步方法。
可以看出,我正在await等待实际执行(也就是从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;
      }
设备和控制台的输出
您似乎有this.setState一个return声明之后。return语句后将不执行任何代码。只需将this.setState呼叫放在返回块之前
同样,该函数getProfileHeightStandardfromDB()必须是一个async函数或需要返回a Promise。当前,该方法不返回a,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);
  });      
}
| 归档时间: | 
 | 
| 查看次数: | 2048 次 | 
| 最近记录: |