Courses

React.js + Laravel: SPA CRUD with Auth

Categories Dropdown and Refactoring into Service

Previous: Post Create Form and Handle Methods
avatar

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
	
avatar
You can use Markdown
avatar
You can use Markdown