mercredi 13 février 2019

Laravel React MySQL Routing Issue

I am trying to make it so that I can register a user but I am getting a 404 error, assuming that means that react is unable to find the route established by the api.php file, is there anything else that I am missing? I have already set it in the package.json file that the proxy is set to "localhost:8000" (the port I chose to use for laravel's backend stuff). I am confused on why it's not hitting this route upon submitting. I feel like I'm close but I am new to using php as the backend so any insight would be helpful.

I am also creating something where the user is able to play music on the app, so there is a route for that labeled "shop", and that does not work for the sole reason that I have not set that route up (also gives a 404 error).

Below are my api routes that I am trying to get react to detect

<?php

Route::post('register','UserController@register');
Route::post('login','UserController@login');
Route::post('profile','UserController@getAuthenticatedUser');

Route::middleware('auth:api')->get('/user', function(Request $request){
    return $request->user();
});

?>

And this is the React portion of my registration file.

  import React, { Component } from 'react';
import { register } from './UserFunctions';

class Register extends Component {
    constructor() {
        super()

    this.state = {
        first_name: '',
        last_name: '',
        email: '',
        password: '',
        errors: {},
    }

    this.onChange = this.onChange.bind(this)
    this.onSubmit = this.onSubmit.bind(this)
}

onChange(e) {
    this.setState({ [e.target.name]: e.target.value })
}

onSubmit(e) {
    e.preventDefault()

    const newUser = {
        name: this.state.first_name + ' ' + this.state.last_name,
        email: this.state.email,
        password: this.state.password
    }

    register(newUser).then(res => {
        if (res) {
            this.props.history.push('/login')
        }
    })
}

render() {
    return (
        <div className="container">
            <div className="row">
                <div className="col-md-6 mt-5 mx auto">
                    <form noValidate onSubmit={this.onSubmit}>
                        <h1 className="h3 mb-3 font-wieght-normal">
                            Register
                        </h1>

                        <div className="form-group">

                            <label htmlFor="first_name">First Name</label>
                            <input type="text" className="form-control" name="first_name" placeholder="Enter First Name" value={this.state.first_name} onChange={this.onChange} />

                            <label htmlFor="last_name">Last Name</label>
                            <input type="text" className="form-control" name="last_name" placeholder="Enter Last Name" value={this.state.last_name} onChange={this.onChange} />

                            <label htmlFor="email">Email Address</label><br />
                            <input type="email" className="form-control" name="email" placeholder="Enter Email" value={this.state.email} onChange={this.onChange} />
                            <br />
                            <label htmlFor="password">Desired Password</label><br />
                            <input type="password" className="form-control" name="password" placeholder="Enter Password" value={this.state.password} onChange={this.onChange} />
                        </div>
                        <button type="submit" className="btn btn-lg btn-primary btn-block">Register</button>
                    </form>
                </div>
            </div>
        </div>
    )
}
}

export default Register

I have those post routes defined, but when I press submit within my register form, I get a 404 error saying that it can't find this route.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire