When I type to search for a certain event or host I get an error.
this is where this search and filtering functionality is (error is located here)
handleSearch = query => {
this.setState({ searchQuery: query });
this.getPagedData();
};
getPagedData = () => {
const { searchQuery, events: allEvents } = this.state;
let filtered = allEvents;
if (searchQuery) {
filtered = allEvents.filter(
e =>
e.title.toLowerCase().startsWith(searchQuery.toLowerCase()) ||
e.hostName.toLowerCase().startsWith(searchQuery.toLowerCase())
);
}
if (searchQuery.length === 0 || searchQuery.length === 1) {
this.setState({
events: getEvents()
});
} else {
this.setState({
events: filtered
});
}
return { totalCount: filtered.length };
};
SearchBox file:
const SearchBox = ({ value, onChange }) => {
return (
<div className="search-box">
<input
className="search-txt"
type="text"
name="query"
placeholder="search"
value={value}
onChange={e => onChange(e.currentTarget.value)}
/>
<a className="search-btn" href="">
<i className="fa fa-search" />
</a>
</div>
);
};
Search Component:
<SearchBox
value={this.state.searchQuery}
onChange={this.handleSearch}
/>
fakeEvents file where the events are located:
const events = [
{
_id: "1",
eventPicture: "event1.jpeg",
hostID: "111",
hostPicture: "profile1.jpg",
eventTime: "Aug 1, Thu 8:00pm",
title: "Basketball",
numberOfAtendies: "12 people are attending",
location: "5 miles away",
details:
"this is a 5 on 5 basketball game and I am looking for advanced players best 2 games out of 3 this is a 5 on 5 basketball game and I am looking for advanced players best 2 games out of 3 this is a 5 on 5 basketball game and I am looking for advanced players best 2 games out of 3.",
category: { _id: "1and1", name: "Sports" },
liked: ""
},
fakeUsers file where the user info comes from:
const users = [
{
_id: "111",
name: "Sami Baghban",
age: "20",
picture: "profile1.jpg",
interests: [
"Basketball",
"Soccer",
"Movies",
"Coding",
"Shopping",
"Football",
"Hiking"
],
discription:
"Lorem ipsum dolor sit amet consectetur adipisicing elit. Fugiat maiores non aliquid pariatur iste aspernatur sapiente sunt voluptatem necessitatibus, nostrum eaque nulla alias porro nisi quisquam tempora minima cupiditate quidem!",
numOfFriends: 400,
numOfEvents: 50
},
State of the events file:
class Events extends Component {
state = {
events: getEvents(),
user: getUser(),
users: getUsers(),
showDetails: false,
shownEventID: 0,
showUserProfile: false,
shownUserID: 0,
searchQuery: ""
};
Error Message:
TypeError: Cannot read property 'toLowerCase' of undefined
allEvents.filter.e
src/components/events.jsx:108
105 |
106 | let filtered = allEvents;
107 | if (searchQuery) {
> 108 | filtered = allEvents.filter(
| ^ 109 | e =>
110 | e.title.toLowerCase().startsWith(searchQuery.toLowerCase()) ||
111 | e.hostName.toLowerCase().startsWith(searchQuery.toLowerCase())
titleor an undefinedhostNamecan you post what the events looks like? @Jbluehdorn the exception is raised from inside the filter, soallEventswould be defined.this.state.events. For one reason or another, it is an array but the entries inside are not what you are expecting.console.logand find out whatallEventsactually is