Skip to content

Commit

Permalink
Handle issues arising from objects in data structure by throwing error (
Browse files Browse the repository at this point in the history
gregnb#921)

* Handle issues arrising from objects in data structure by throwing error

* Better attempt takes null and array into account, which are allowed
  • Loading branch information
gabrielliwerant authored and wuhuizuo committed Oct 22, 2019
1 parent e5396fc commit 02482ea
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/MUIDataTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ class MUIDataTable extends React.Component {
transformData = (columns, data) => {
const leaf = (obj, path) => path.split('.').reduce((value, el) => (value ? value[el] : undefined), obj);

return Array.isArray(data[0])
const transformedData = Array.isArray(data[0])
? data.map(row => {
let i = -1;

Expand All @@ -464,6 +464,12 @@ class MUIDataTable extends React.Component {
});
})
: data.map(row => columns.map(col => leaf(row, col.name)));

// We need to determine if object data exists in the transformed structure, as this is currently not allowed
const hasInvalidData = transformedData.filter(data => data.filter(d => typeof d === 'object' && d !== null && !Array.isArray(d)).length > 0).length > 0;
if (hasInvalidData) throw Error(`Cannot accept objects for cell data. Cells need to contain strings | numbers. It\'s possible this error is the result of a missing dot in the column name field (e.g. name: "company" instead of name: "company.id")`);

return transformedData;
};

setTableData(props, status, callback = () => {}) {
Expand Down

0 comments on commit 02482ea

Please sign in to comment.