dosubot[bot] commented on issue #29158:
URL: https://github.com/apache/superset/issues/29158#issuecomment-2157958637
<!-- Greeting -->
Hey @ThinneshHummingsoft! I'm here to help you with your bug report. I can
assist you with solving bugs, answering questions, and even becoming a
contributor. Let's get to the bottom of this together!
<!-- Answer -->
To resolve the "Unexpected error: TypeError: Cannot read properties of
undefined (reading 'some')" error when adding metrics in the tree chart page of
Apache Superset, ensure that the `savedMetrics` and `columns` props passed to
the `DndMetricSelect` component are properly defined and not `undefined`.
The error occurs because the `some` method is being called on `undefined` in
the `coerceMetrics` function. This can happen if `savedMetrics` or `columns`
are not properly initialized.
Here is the relevant part of the code where the error might be occurring:
```tsx
const coerceMetrics = (
addedMetrics: QueryFormMetric | QueryFormMetric[] | undefined | null,
savedMetrics: Metric[],
columns: ColumnMeta[],
) => {
if (!addedMetrics) {
return [];
}
const metricsCompatibleWithDataset = ensureIsArray(addedMetrics).filter(
metric => {
if (isSavedMetric(metric)) {
return savedMetrics.some(
savedMetric => savedMetric.metric_name === metric,
);
}
if (isAdhocMetricSimple(metric)) {
return columns.some(
column => column.column_name === metric.column.column_name,
);
}
return true;
},
);
return metricsCompatibleWithDataset.map(metric => {
if (!isDictionaryForAdhocMetric(metric)) {
return metric;
}
if (isAdhocMetricSimple(metric)) {
const column = columns.find(
col => col.column_name === metric.column.column_name,
);
if (column) {
return new AdhocMetric({ ...metric, column });
}
}
return new AdhocMetric(metric);
});
};
```
To fix this, ensure that `savedMetrics` and `columns` are always defined
when passed to the `DndMetricSelect` component. You can add default values or
checks to handle cases where these might be `undefined`.
For example:
```tsx
const DndMetricSelect = (props: any) => {
const { onChange, multi, datasource, savedMetrics = [], columns = [] } =
props;
// ... rest of the component code
};
```
By providing default values (`[]` for both `savedMetrics` and `columns`),
you can prevent the `some` method from being called on `undefined`, thus
avoiding the TypeError
<sup>[[1]](https://github.com/apache/superset/blob/master/superset-frontend/src/explore/components/controls/DndColumnSelectControl/DndMetricSelect.tsx)</sup>.
<!-- Dosu Comment Footer -->
*To continue talking to [Dosu](https://dosu.dev), mention @dosu.*
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]