react-bootstrap-table icon indicating copy to clipboard operation
react-bootstrap-table copied to clipboard

TypeError keep throwing if renew data after delete all rows in table

Open XidongHuang opened this issue 9 years ago • 13 comments

Hi, I have a problem here: The table will throw Uncaught (in promise) TypeError: Cannot read property 'coreId' of undefined if I renew the data source after delete all rows (not matter delete one by one or delete all of them once), but it works fine after I re-fresh page even the table is empty. The Error screen shot:

1

2

Also, the table is working well for adding (renew data source), multi-delete rows (just not delete all). And the way I am working on it is:

...
remoteDelete = (row) => {
          
        let settleFlag = 1;
        let settleInfo = {coreId: row};
        this.props.registerDevice(settleInfo, settleFlag).then(
            (res) => {
                // --> renew data source from database after deleted rows
                this.setState({dataArr: res.data.devices, unsettledDevices: res.data.unsettledDevices});
            },
            (err) => {
                console.error(err);
            }
        );
        console.log('delete', settleInfo);
    }

onAddRow = (row) => {
        let settleFlag = 0;
       
        this.props.registerDevice(row, settleFlag).then(
            (res) => {
                // --> renew data source from database after adding row
                this.setState({dataArr: res.data.devices, unsettledDevices: res.data.unsettledDevices});
            },
            (err) => {
                  console.error(err);
            }
        );
    }

options = {
        // paginationPosition:"top",
        paginationShowsTotal: this.renderPaginationShowsTotal,
        sizePerPageList: ['10',  '30', '50', '60'],
        sizePerPage: 10,
        sizePerPageDropDown: this.renderSizePerPageDropDown,
        // exportCSVBtn: this.createCustomExportCSVButton,
        defaultSortName: 'coreId',
        defaultSortOrder: 'asc',
        
        handleConfirmDeleteRow: this.confirmDelete,
        onDeleteRow: this.remoteDelete,

        insertModalHeader: this.modalHeader,
        insertModalBody: this.modalBody,

        insertText: 'register',
        deleteText: 'unregister',
        
        onAddRow: this.onAddRow,   
    }

...
render() {
        const {dataArr, errors, delModalOpen, delNext, delDeviceId} = this.state;
        let data = _.isEmpty(dataArr)? [] : dataArr;
        console.log('device render', data);  //--> The data is correct when the error comes
        const confirmMegs = `Do you want to unregister device:`;
        return (
            <div>
                <BootstrapTable data={data} striped hover search 
                    condensed pagination insertRow deleteRow remote={this.remote}  options={this.options} selectRow={this.selectRowProp}>

                    <TableHeaderColumn isKey tdStyle={{whiteSpace: 'normal', 'wordWrap': 'break-word'}} 
                        thStyle={{whiteSpace: 'normal'}} dataFormat={this.coreIdLink} dataField="coreId" dataSort>Core ID</TableHeaderColumn>
                    ...
                </BootstrapTable>
            </div>
}

I check the data source when it renew (adding or deleting), they look same, so are there any errors in setting or usage of the table? Please give me some hints. Thank you.

XidongHuang avatar Jul 21 '17 14:07 XidongHuang

@XidongHuang sorry for lately reply, few questions

  1. I wanna check your this.remote function

if I renew the data source after delete all rows (not matter delete one by one or delete all of them once),

How you renew the data? By onAddRow function? if yes, please try to return in the end;

onAddRow = (row) => {
        let settleFlag = 0;
       
        this.props.registerDevice(row, settleFlag).then(
            (res) => {
                // --> renew data source from database after adding row
                this.setState({dataArr: res.data.devices, unsettledDevices: res.data.unsettledDevices});
            },
            (err) => {
                  console.error(err);
            }
        );
        return 'some error';   // < return error message sync if there're some error
        return;  // << return nothing means no error, just execute directly, you should use this one
        return false;  // << means your error message will be handled as async, please check following
}

BTW, in the onAddRow your operation is async, so you can have some improvement on handling the error message async, you can check this

It's a patch on this month, #1391. Please use it if it's helpful to you. Please let me know if there's any unclear for my description, thanks

Also let me know if the bug still remain.

AllenFang avatar Jul 23 '17 02:07 AllenFang

Hi @AllenFang ,

  1. my this.remote here is :
...
remote(remoteObj) {
    remoteObj.insertRow = true;
    remoteObj.dropRow = true;
    return remoteObj;

}
  1. For renewing data in this problem, I have two ways to do that, One is after delete row(s), a new data objects array will come from response:
remoteDelete = (row) => {
          
        let settleFlag = 1;
        let settleInfo = {coreId: row};
        this.props.registerDevice(settleInfo, settleFlag).then(
            (res) => {
                // --> renew data source from database after deleted row(s)
                this.setState({dataArr: res.data.devices, unsettledDevices: res.data.unsettledDevices});
            },
            (err) => {
                console.error(err);
            }
        );
        console.log('delete', settleInfo);
    }

Another way is use onAddRow method directly.

Problem: The table works fine just show There is no data to display, if I deleted all rows (no matter once or one by one) and not data (empty object array) come back. There images are from chrome plugin react 2-1 2-2

However, if a new data objects array come from the remoteDelete or onAddRow response when the table is empty (just after delete all), then throw error Uncaught (in promise) TypeError: Cannot read property 'coreId' of undefined. The status of table when the error comes out: 3-1 3-2 As we can see, the table's data is updated, and coreId exists, but the error still coming out.

The reference you tag there is about handling error in onAddRow method, like keep the modal if there are errors. I tried to set:

onAddRow = (row) => {
        let settleFlag = 0;
       
        this.props.registerDevice(row, settleFlag).then(
            (res) => {
                // --> renew data source from database after adding row
                this.setState({dataArr: res.data.devices, unsettledDevices: res.data.unsettledDevices});
            },
            (err) => {
                  console.error(err);
            }
        );
 
        //return; 
        return false; 
        /**
           I tried both, but it still throw the error
        */
}

Thanks for your hard working!

XidongHuang avatar Jul 24 '17 15:07 XidongHuang

@XidongHuang sorry for lately reply and let me clear your question currently,,

  1. you have a table with remote enable but just only for insertRow and dropRow
  2. When delete all trigger, you call an API by promise-way
  3. if the response of API have some data, you will renew those data into table again
  4. you got the error

AllenFang avatar Jul 26 '17 07:07 AllenFang

I think it may a bug, but I need to test it, if confirm, I will let you know, thanks

AllenFang avatar Jul 26 '17 07:07 AllenFang

I am experiencing the same issue. Using remote, if I delete all rows, and try to add a new one I have the same error. Using v4.0.0-beta.9 btw.

@AllenFang It doesn't matter if I try to update the state from promise or directly in the onAddRow event hook.

aesmon avatar Aug 04 '17 14:08 aesmon

HI @aesmon, how did you add news row into table? Due to I still cant reproduce this.

AllenFang avatar Aug 08 '17 12:08 AllenFang

I use setTimeout to simulate the async operation for add/delete rows, but still can not reproduce it. Anything I doing wrong?

onAddRow = (row) => {
    setTimeout(() => {
      const { data } = this.state;
      data.push(row);
      this.setState({ data });
    }, 1000);
    return;
  }

  onDeleteRow = (row) => {
    setTimeout(() => {
      const data = this.state.data.filter((product) => {
        return row.indexOf(product.id) === -1;
      });

      this.setState({ data });
    }, 1000);
  }

AllenFang avatar Aug 08 '17 13:08 AllenFang

Hi @AllenFang sorry for the late reply, I must have missed the notification.

It only happens when you delete all/last row and try to add a new one. The error only happens when the table is emptied before adding a row. If I refresh, get the empty table again and try to add the row it works fine. Maybe the issue lays somewhere within when the collection of rows is emptied? I haven't had time to look into the source code to debug :(

My functions

onAddRow = (row) => {
        const opts = {
            _csrf: window.Laravel.csrfToken,
            url: laroute.route(this.route_prefix+'myAddRoute', {'id': current_user_edited}),
            data: row
        };
        $.post(opts, (data)=> {})
            .done(data => {
                const rev = row;
                if(data['status'] == 'success'){
                    rev.id = data['id'];
                    this.revenue.push(rev);
                    this.setState({
                        revenue: this.revenue
                    });
                    toastr.success('x', 'x', 50000);
                } else {
                    toastr.error(data.message, 'x', 50000);
                }
            });
    };


    onDeleteRow = (rows) => {
        const opts = {
            _csrf: window.Laravel.csrfToken,
            url: laroute.route(this.route_prefix+'myDeleteRoute', {'id': current_user_edited}),
            data: {rows}
        };
        $.post(opts, data=> {})
            .done(data=> {
                if (data.status == 'success') {
                    this.revenue = this.revenue.filter((n) => {
                        return _.indexOf(rows, n['id']) == -1;
                    });
                    this.setState({revenue: this.revenue});
                    toastr.success('x', 'x', 50000);
                } else {
                    toastr.error(data.message, 'x', 50000);
                }
            });
    };

My React version - [email protected]

aesmon avatar Sep 14 '17 13:09 aesmon

Hi @AllenFang , I am encountering the same problem when I try to add new row after deleting all existing rows. My code has no remote() being used. My code is as following:

onAddRow(row) {
     if(!row[prop]) {
        return prop + " is missing";
      }
    }
    addRow(row);
}

From my guess, the bug should be caused by the TableDataStore (I'm not sure, just guessing from the documentation). Thus, I add a re-rendering in onAfterDeleteRow, like this:

onAfterDeleteRow(rowKeys){
    rowKeys.map(rowKey => {
      removeRow(rowKey);
    });
    this.setState(this.state);
}

This is a temporary fix for this problem from my implementation, and will cause re-rendering, and this will be the drawback by this quick fix.

Still, thank you for contributing the community and very obliged to use your React Bootstrap Table, it is so good! Also your hard-work to reply issues!

thminggg avatar Dec 04 '17 03:12 thminggg

HI @thminggg what's thing you do when your addRow function, is that synchronize function? Please describe your addRow function, thanks

AllenFang avatar Dec 05 '17 14:12 AllenFang

Hi @AllenFang, sorry for late reply, busy on work.

Actually, I am using Meteor + React for the current project. So there might be something different from others' situations.

The addRow function is a Meteor method call, which triggers MongoDB to insert 1 document to a specific user.

Once a new document is inserted, Meteor will notice the change in MongoDB and automatically update the props on the client side, and then the new data will be shown accordingly.

Up to this point, since MongoDB is the "starter" which updates client side props, so I would consider it is a synchronize function.

addRow: Meteor method call --> insert to MongoDB --> Meteor notices change in MongoDB --> update client side props --> new data being shown

Sorry I didn't provide the details to you in the last comment, and thank you once again for your hard-work, really appreciate the project!

thminggg avatar Dec 06 '17 14:12 thminggg

@thmingg sorry for lately reply, I still can not reproduce this kind of issue, but you are suppose to use remote. however, I'm expect there's any workable code to reproduce this issue, it's will be very appreciated :)

AllenFang avatar Dec 09 '17 07:12 AllenFang

@AllenFang Thanks so much for updating and your checking. I am going to re-examine my code, and try to provide a minimal setting to re-produce the error. But it may take me a long time since the project is still growing now. Thanks! 👍

thminggg avatar Dec 09 '17 09:12 thminggg