0

I'm working with react-native-db-models node module. I have 3 rows on my database model. => ( title | privateid | action )

I lose 2 days for this workout. I want to list all records from database. What's wrong?

var Download = React.createClass({
    getInitialState: function () {
      return {
        dataSource: new ListView.DataSource({
          rowHasChanged: (row1, row2) => row1 !== row2,
        })
      };
    },

    componentDidMount: function() {
      this.fetchData();
    },

    fetchData: function () {
      DB.downloads.get_all(function(result) {
         var data = [];
         for(var i = 1; i <= result.totalrows; i++) {
           console.log(result.rows[i]);
           data[i-1] = result.rows[i];
           this.setState({
             dataSource: dataSource.cloneWithRows(result.rows[i]),
             loaded:false,
           })
         }

       });

    },
    render: function () {
      return (
        <ListView
         dataSource={this.state.dataSource}
         renderRow={this.renderTrack}
         style={styles.listView}/>
      );
    },
    renderTrack: function (track) {
      return (
        <View>
          <Text>{track.title}</Text>
        </View>
      );
    }
  });
Pang
  • 9,564
  • 146
  • 81
  • 122
Prath Prat
  • 71
  • 4
  • Maybe you could give us more of an idea where it's gone south. Are you getting anything back from `DB.downloads.get_all(function(result)`? What happens if you console.log(result)? I think this... dataSource.cloneWithRows(result.rows[i]) should be this... `dataSource.cloneWithRows(data)` and how you're building the data array looks odd to me too. At the very least shouldn't it be `data.push(results.row[i])`? This assumes that `console.log(result.rows[i]);` returns an object. Hope that helps, but more info would really help me help you – Chris Geirman Oct 30 '15 at 03:18
  • @Chris, console.log(result) is working good. console.log(data) and console.log(result.rows[i]) working good in "for" function but i setState is not working in "for" function. when i do outside "for", its returning "undefineded" :| – Prath Prat Oct 30 '15 at 13:26
  • There seems to be a lot of this going around right now. See if this helps you out... http://stackoverflow.com/questions/33426760/react-native-listview-not-updating-on-data-change/33428564?noredirect=1#comment54667379_33428564 – Chris Geirman Oct 31 '15 at 02:59

1 Answers1

0

Do this...

  fetchData: function () {
  DB.downloads.get_all(function(result) {
     var data = [];
     for(var i = 1; i <= result.totalrows; i++) {
       data.push(result.rows[i]);
     }
     // Outside of the loop, apply the data
       this.setState({
         dataSource: dataSource.cloneWithRows(data),
         loaded:false,
       })
   });

},
Kylie
  • 11,421
  • 11
  • 47
  • 78