mui-datatables icon indicating copy to clipboard operation
mui-datatables copied to clipboard

default filter is throwing TypeError: Cannot read property 'toString' of undefined

Open dyoung522 opened this issue 8 years ago • 13 comments

The following code snippet displays the DataTable properly, but then throws TypeError: Cannot read property 'toString' of undefined when the 'filter' icon is clicked.

Not sure if this is a bug, or I'm missing something.

    const tableOptions = {
      filter: true,
      pagination: false,
      download: false,
      selectableRows: false,
      print: false,
      viewColumns: false
    }

    const charColumns = [
      { name: 'ID', options: { display: false, filter: false, sort: false, download: true } },
      { name: 'Name', options: { display: true, filter: true, sort: true, download: true } },
      { name: 'Level', options: { display: true, filter: true, sort: true, download: true } },
      { name: 'Race', options: { display: true, filter: true, sort: true, download: true } },
      { name: 'Class', options: { display: true, filter: true, sort: true, download: true } },
      { name: 'Sex', options: { display: true, filter: true, sort: true, download: true } },
      { name: 'Owner', options: { display: true, filter: true, sort: true, download: true } }
    ]

    // Creates an array of arrays from Firestore data
    const charData =
      isLoaded(characters) && isLoaded(users)
        ? Object.keys(characters).map(id => {
          const char = characters[id]

          return isEmpty(char)
            ? []
            : [id, char.name, char.level, char.race, char.class, char.sex, users[char.owner].displayName]
        }).sort((a, b) => a[1] - b[1])
        : [['loading', 'Loading... please wait']]

    return (
        <Paper className={classes.root}>
          <MUIDataTable title={'Guild Roster'} data={charData} columns={charColumns} options={tableOptions} />
        </Paper>
    )

Here's the raw charData:

[Array(7)]
0: (7) ["5GM0mnQs2xZP1zCms9xK", "Squall", "5", "Tabaxi", "Monk", "Male", "Donovan C. Young"]

Your Environment

Tech Version
Material-UI 3.6.0
MUI-datatables 2.0.0-beta-41
React 16.6.3
browser Chrome
etc react-redux-firebase v.2.2.4

dyoung522 avatar Dec 02 '18 20:12 dyoung522

Interestingly, if I use the raw charData array (hardcoded) it works. So there must be something about the way I'm creating the charData above, but I haven't see any differences in the console. Both seem to produce the identical array or arrays.

dyoung522 avatar Dec 02 '18 20:12 dyoung522

There's no difference in the two arrays, I've programmatically compared them item for item and they're identical, so there's something else in play here.

Here's the trace (if it helps)

index.js:404 Uncaught TypeError: Cannot read property 'toString' of undefined
    at index.js:404
    at Array.map (<anonymous>)
    at index.js:389
    at Array.map (<anonymous>)
    at l.value (index.js:382)
    at l.value (index.js:524)
    at finishClassComponent (react-dom.development.js:14899)
    at updateClassComponent (react-dom.development.js:14863)
    at beginWork (react-dom.development.js:15716)
    at performUnitOfWork (react-dom.development.js:18750)
    at workLoop (react-dom.development.js:18791)
    at HTMLUnknownElement.callCallback (react-dom.development.js:147)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:196)
    at invokeGuardedCallback (react-dom.development.js:250)
    at replayUnitOfWork (react-dom.development.js:17998)
    at renderRoot (react-dom.development.js:18909)
    at performWorkOnRoot (react-dom.development.js:19812)
    at performWork (react-dom.development.js:19722)
    at performSyncWork (react-dom.development.js:19696)
    at interactiveUpdates$1 (react-dom.development.js:19984)
    at interactiveUpdates (react-dom.development.js:2267)
    at dispatchInteractiveEvent (react-dom.development.js:5081)
(anonymous) @ index.js:404
(anonymous) @ index.js:389
value @ index.js:382
value @ index.js:524
finishClassComponent @ react-dom.development.js:14899
updateClassComponent @ react-dom.development.js:14863
beginWork @ react-dom.development.js:15716
performUnitOfWork @ react-dom.development.js:18750
workLoop @ react-dom.development.js:18791
callCallback @ react-dom.development.js:147
invokeGuardedCallbackDev @ react-dom.development.js:196
invokeGuardedCallback @ react-dom.development.js:250
replayUnitOfWork @ react-dom.development.js:17998
renderRoot @ react-dom.development.js:18909
performWorkOnRoot @ react-dom.development.js:19812
performWork @ react-dom.development.js:19722
performSyncWork @ react-dom.development.js:19696
interactiveUpdates$1 @ react-dom.development.js:19984
interactiveUpdates @ react-dom.development.js:2267
dispatchInteractiveEvent @ react-dom.development.js:5081
ModalManager.js:71 Uncaught TypeError: Cannot convert undefined or null to object
    at Function.keys (<anonymous>)
    at removeContainerStyle (ModalManager.js:71)
    at ModalManager.remove (ModalManager.js:180)
    at Modal._this.handleClose (Modal.js:144)
    at Modal.componentWillUnmount (Modal.js:244)
    at callComponentWillUnmountWithTimer (react-dom.development.js:16540)
    at HTMLUnknownElement.callCallback (react-dom.development.js:147)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:196)
    at invokeGuardedCallback (react-dom.development.js:250)
    at safelyCallComponentWillUnmount (react-dom.development.js:16547)
    at commitUnmount (react-dom.development.js:16976)
    at commitNestedUnmounts (react-dom.development.js:17012)
    at unmountHostComponents (react-dom.development.js:17294)
    at commitDeletion (react-dom.development.js:17351)
    at commitAllHostEffects (react-dom.development.js:18110)
    at HTMLUnknownElement.callCallback (react-dom.development.js:147)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:196)
    at invokeGuardedCallback (react-dom.development.js:250)
    at commitRoot (react-dom.development.js:18327)
    at completeRoot (react-dom.development.js:19894)
    at performWorkOnRoot (react-dom.development.js:19817)
    at performWork (react-dom.development.js:19722)
    at performSyncWork (react-dom.development.js:19696)
    at interactiveUpdates$1 (react-dom.development.js:19984)
    at interactiveUpdates (react-dom.development.js:2267)
    at dispatchInteractiveEvent (react-dom.development.js:5081)

dyoung522 avatar Dec 02 '18 22:12 dyoung522

So... I figured out what's happening, but not why.

While data is still loading, we return this:

[['loading', 'Loading... please wait']]

Then, when the data loads, we supply the real data.

It seems that MUIDataTable is storing that original array somewhere, and because this "Loading" array didn't have all elements, it was causing that problem. I filled in the remaining elements with blank strings and it started working.

However, even after the data loads, and all we see in the table is the actual data, the filter is still showing the results of the initial "Loading" array.

That clearly seems to be a bug.

dyoung522 avatar Dec 02 '18 23:12 dyoung522

This error also occurs with 'null' values

hwatersiv avatar Dec 14 '18 18:12 hwatersiv

Hitting the same issue as well with undefined

zwk5004 avatar Jan 16 '19 03:01 zwk5004

I keep getting distracted from making a PR; however, for at least the non-remote version of Uncaught TypeError: Cannot convert undefined or null to object I believe the following lines in TableFilter.js are the offenders: Line 176 Line 210 Line 265

ideoclickVanessa avatar Jan 16 '19 18:01 ideoclickVanessa

I believe I'm running into this as well, is anyone aware of a resolution or workarounds?

aprakash-sovrn avatar Feb 19 '19 22:02 aprakash-sovrn

finally solved the problem by filling empty strings into the data array when the actual data is not retrieved from the server yet. for us, the bug happens when the "data" is [[],[]] but the 'columns' has value e.g ['column1','column2']. In this case, it tries to get row data from []. what you need is [['',''],['','']].

chennanoka avatar Feb 22 '19 20:02 chennanoka

@dyoung522 I attempted to recreate the issue here https://codesandbox.io/s/132kpqoxk3?fontsize=14, using setTimeout to mimic a response from a server. I do not experience the issue with this example, so there may be something more nuanced with what you're doing, or the issue might have been fixed in the mean time.

Can you take a look and see if you can break what I did or see if you can integrate my approach into your code and let me know how that goes?

gabrielliwerant avatar Apr 27 '19 00:04 gabrielliwerant

Closing this issue as it seems to be fixed and there hasn't been any new activity here in a while. If the issue crops up again, please open a new issue with a reproducible example.

gabrielliwerant avatar May 07 '19 20:05 gabrielliwerant

@gabrielliwerant I ran into this again with 2.3.0. The workaround is to pad the data empty strings rather than undefined values. You can reproduce the error by clicking on the filter button (configured for multiselect). https://codesandbox.io/embed/muidatatables-crash-multiselect-filters-undefined-data-fnfbe

ericpyle avatar Jun 05 '19 20:06 ericpyle

@ericpyle Thank you, you're right, undefined was an edge case that was not being covered. New fix available here https://github.com/gregnb/mui-datatables/pull/669.

gabrielliwerant avatar Jun 05 '19 21:06 gabrielliwerant

Should be fixed again in 2.4.0.

gabrielliwerant avatar Jun 20 '19 04:06 gabrielliwerant