Skip to main content

Categories Dropdown and Refactoring into Service

Premium
5:23

The Full Lesson is Only for Premium Members

Want to access all of our courses? (30 h 41 min)

You also get:

55 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

EO
Ebenezer Oyenuga ✓ Link copied!

Thanks for this tutorial it has been enrishing so far, however I am currently stock at this point. I am getting the error: Uncaught TypeError: Cannot read properties of undefined (reading 'map') on the posts/create page for the line that fetches the categories to the dropdownlist box. My code is below:

constructor(props) { super(props);

    this.state = {
        title: '',
        content: '',
        category_id: '',
        categories: []
    }

    this.handleTitleChange = this.handleTitleChange.bind(this);
    this.handleContentChange = this.handleContentChange.bind(this);
    this.handleCategoryChange = this.handleCategoryChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
}
	
	componentDidMount() {
    CategoryService.getAll().then(response => this.setState({ categories: response.data.data }));

}
	
	the conder in the render form:
	<select value={this.state.category_id} onChange={this.handleCategoryChange} id="category" className="block mt-1 w-full rounded-md shadow-sm border-gray-300 focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50">
                    <option value="">-- Select category --</option>
                    {this.state.categories.map((category, index) => (
                        <option key={index} value={category.id}>{category.name}</option>
                    ))}
      </select>
				
				Everything seems ok also with the github could there be anything I am missen or could it be because I am using Inertia to load my page? Your response will be highly appreciated
	

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.