Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sqlite React本机函数不返回任何内容_Sqlite_React Native - Fatal编程技术网

Sqlite React本机函数不返回任何内容

Sqlite React本机函数不返回任何内容,sqlite,react-native,Sqlite,React Native,我在这里尝试做一些简单的事情——连接到sqlite数据库并返回记录数。在下面的代码中,我可以使用console.log输出len变量,但没有返回任何内容。我错过了什么明显的东西吗?谢谢 const db = SQLite.openDatabase({ name: 'favorites.db' }); export default class PlayerScreen extends React.Component { fetch(){ console.log('fetchin

我在这里尝试做一些简单的事情——连接到sqlite数据库并返回记录数。在下面的代码中,我可以使用console.log输出len变量,但没有返回任何内容。我错过了什么明显的东西吗?谢谢

const db = SQLite.openDatabase({ name: 'favorites.db' });
export default class PlayerScreen extends React.Component {

   fetch(){
      console.log('fetching data from database');
       var query = "SELECT * FROM items";
       var params = [];
         db.transaction((tx) => {
            tx.executeSql(query,params, (tx, results) => {
            var len = results.rows.length;
            return len;

          }, function(){
            console.log('Profile: Something went wrong');
          });
      });

}

render() {

   return  <Text>{this.fetch()}</Text>

}
 }
const db=SQLite.openDatabase({name:'favorites.db'});
导出默认类PlayerScreen扩展React.Component{
fetch(){
log(“从数据库获取数据”);
var query=“从项目中选择*”;
var参数=[];
数据库事务((tx)=>{
tx.executeSql(查询,参数,(发送,结果)=>{
var len=results.rows.length;
回程透镜;
},函数(){
log('Profile:Something出错');
});
});
}
render(){
返回{this.fetch()}
}
}

这里有很多错误。Fetch是一个异步概念,React呈现是同步的。不能在render方法内异步使用fetch或任何东西

那么,你应该怎么做呢

首先,在组件的componentDidMount中进行提取。 还可以在componentWillMount中设置初始状态

componentWillMount() {
  this.state = { length: 0 };
}

componentDidMount() {
  // I use call here to make sure this.fetch gets the this context
  this.fetch.call(this);
}
其次,将结果附加到fetch方法中组件的innerState

fetch() {
  var query = "SELECT * FROM items";
   var params = [];
     db.transaction((tx) => {
        tx.executeSql(query,params, (tx, results) => {
        var len = results.rows.length;

        // this.setState will trigger a re-render of the component
        this.setState({ length: len }); 

      }, function(){
        console.log('Profile: Something went wrong');
      });
  });
}
现在,在render方法中,可以使用this.state.length渲染值

render() {
  return  <Text>{this.state.length}</Text>
}
render(){
返回{this.state.length}
}

希望这对你有所帮助

这里很多事情都不对劲。Fetch是一个异步概念,React呈现是同步的。不能在render方法内异步使用fetch或任何东西

那么,你应该怎么做呢

首先,在组件的componentDidMount中进行提取。 还可以在componentWillMount中设置初始状态

componentWillMount() {
  this.state = { length: 0 };
}

componentDidMount() {
  // I use call here to make sure this.fetch gets the this context
  this.fetch.call(this);
}
其次,将结果附加到fetch方法中组件的innerState

fetch() {
  var query = "SELECT * FROM items";
   var params = [];
     db.transaction((tx) => {
        tx.executeSql(query,params, (tx, results) => {
        var len = results.rows.length;

        // this.setState will trigger a re-render of the component
        this.setState({ length: len }); 

      }, function(){
        console.log('Profile: Something went wrong');
      });
  });
}
现在,在render方法中,可以使用this.state.length渲染值

render() {
  return  <Text>{this.state.length}</Text>
}
render(){
返回{this.state.length}
}

希望这对你有所帮助

谢谢…这很有帮助谢谢…这很有帮助