I had this working but I guess I broke something, I am attempting to search by value. I am currently receiving the following error. Uncaught TypeError: Cannot read properties of undefined (reading 'localeCompare')
when running the following code. Im kinda stumped at this point and I just don’t know what to do at this point.
Sample Data at the end..
const handleSearchChange = (event) => {
const { value } = event.currentTarget;
setSearch(value);
setSortedData(sortData(searchData, { sortBy, reversed: reverseSortDirection, search: value }));
};
The error is happening at the Return FilterData.
function sortData(data, payload) {
const { sortBy } = payload;
if (!sortBy) {
return filterData(data, payload.search);
}
return filterData(
[...data].sort((a, b) => {
if (payload.reversed) {
return b[sortBy].localeCompare(a[sortBy]);
}
return a[sortBy].localeCompare(b[sortBy]);
}),
payload.search
);
}
function filterData(data, search) {
const query = search.toLowerCase().trim();
console.log(query)
return data.filter((item) => keys(data[0]).some((key) => item[key].toLowerCase().includes(query)));
}
Sample Data
[
{
"_id": "66675800422b4e2e6ca0dcba",
"country": "Colombia",
"facilityType": "Grower",
"bpFarmerId": "10065877",
"externalId": "10065877",
"createdAt": "2024-06-10T19:46:10.270Z",
"updatedAt": "2024-06-10T19:46:10.270Z",
"__v": 0
},
{
"_id": "66675800422b4e2e6ca0dcbb",
"country": "Colombia",
"facilityType": "Grower",
"bpFarmerId": "10076270",
"externalId": "10076270",
"createdAt": "2024-06-10T19:46:10.270Z",
"updatedAt": "2024-06-10T19:46:10.270Z",
"__v": 0
},
{
"_id": "66675800422b4e2e6ca0dcbc",
"country": "Colombia",
"facilityType": "Grower",
"bpFarmerId": "10235411",
"externalId": "10235411",
"createdAt": "2024-06-10T19:46:10.270Z",
"updatedAt": "2024-06-10T19:46:10.270Z",
"__v": 0
},
{
"_id": "66675800422b4e2e6ca0dcbf",
"country": "Colombia",
"facilityType": "Grower",
"bpFarmerId": "1054918053",
"externalId": "1054918053",
"createdAt": "2024-06-10T19:46:10.270Z",
"updatedAt": "2024-06-10T19:46:10.270Z",
"__v": 0
},
{
"_id": "66675800422b4e2e6ca0dcc0",
"country": "Colombia",
"facilityType": "Grower",
"bpFarmerId": "1054918203",
"externalId": "1054918203",
"createdAt": "2024-06-10T19:46:10.270Z",
"updatedAt": "2024-06-10T19:46:10.270Z",
"__v": 0
},
{
"_id": "66675800422b4e2e6ca0dcc1",
"country": "Colombia",
"facilityType": "Grower",
"bpFarmerId": "12096506",
"externalId": "12096506",
"createdAt": "2024-06-10T19:46:10.270Z",
"updatedAt": "2024-06-10T19:46:10.270Z",
"__v": 0
},
{
"_id": "66675800422b4e2e6ca0dcc2",
"country": "Colombia",
"facilityType": "Grower",
"bpFarmerId": "1263867",
"externalId": "1263867",
"createdAt": "2024-06-10T19:46:10.270Z",
"updatedAt": "2024-06-10T19:46:10.270Z",
"__v": 0
},
{
"_id": "66675800422b4e2e6ca0dcc3",
"country": "Colombia",
"facilityType": "Grower",
"bpFarmerId": "1381787",
"externalId": "1381787",
"createdAt": "2024-06-10T19:46:10.270Z",
"updatedAt": "2024-06-10T19:46:10.270Z",
"__v": 0
},
{
"_id": "66675800422b4e2e6ca0dcc4",
"country": "Colombia",
"facilityType": "Grower",
"bpFarmerId": "14281728",
"externalId": "14281728",
"createdAt": "2024-06-10T19:46:10.270Z",
"updatedAt": "2024-06-10T19:46:10.270Z",
"__v": 0
},
{
"_id": "66675800422b4e2e6ca0dcc5",
"country": "Colombia",
"facilityType": "Grower",
"bpFarmerId": "14455407",
"externalId": "14455407",
"createdAt": "2024-06-10T19:46:10.270Z",
"updatedAt": "2024-06-10T19:46:10.270Z",
"__v": 0
}
]