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>
      );
    }
  });
3
  • 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 Commented Oct 30, 2015 at 3: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" :| Commented Oct 30, 2015 at 13:26
  • There seems to be a lot of this going around right now. See if this helps you out... stackoverflow.com/questions/33426760/… Commented Oct 31, 2015 at 2:59

1 Answer 1

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,
       })
   });

},
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.