简体   繁体   中英

React Axios to Rails Knock

I am trying to send a POST request from axios to a Rails API using the following function in the React frontend:

  export function registerUser({ name, email, password }) {
    var postdata = JSON.stringify({
      auth: {
        name, email, password
        }
      });
    return function(dispatch) {
      axios.post(`${API_URL}/user_token`, postdata )
      .then(response => {
        cookie.save('token', response.data.token, { path: '/' });
        dispatch({ type: AUTH_USER });
        window.location.href = CLIENT_ROOT_URL + '/dashboard';
      })
      .catch((error) => {
        errorHandler(dispatch, error.response, AUTH_ERROR)
      });
    }
  }

The Knock gem expects the request in the following format:

{"auth": {"email": "foo@bar.com", "password": "secret"}}

My current function seem to generate the correct format (inspecting the request in the browser devtools), but I'm getting the following error:

Uncaught (in promise) Error: Objects are not valid as a React child (found: object with keys {data, status, statusText, headers, config, request}). If you meant to render a collection of children, use an array instead or wrap the object using createFragment(object) from the React add-ons. Check the render method of Register .

class Register extends Component {
  handleFormSubmit(formProps) {
    this.props.registerUser(formProps);
  }

  renderAlert() {
    if(this.props.errorMessage) {
      return (
        <div>
          <span><strong>Error!</strong> {this.props.errorMessage}</span>
        </div>
      );
    }
  }

  render() {
    const { handleSubmit } = this.props;

    return (
      <form onSubmit={handleSubmit(this.handleFormSubmit.bind(this))}>
      {this.renderAlert()}
      <div className="row">
        <div className="col-md-6">
          <label>Name</label>
          <Field name="name" className="form-control" component={renderField} type="text" />
        </div>
      </div>
        <div className="row">
          <div className="col-md-12">
            <label>Email</label>
            <Field name="email" className="form-control" component={renderField} type="text" />
          </div>
        </div>
        <div className="row">
          <div className="col-md-12">
            <label>Password</label>
            <Field name="password" className="form-control" component={renderField} type="password" />
          </div>
        </div>
        <button type="submit" className="btn btn-primary">Register</button>
      </form>
    );
  }
}

The error is caused by the following line in your code

errorHandler(dispatch, error.response, AUTH_ERROR)

The exception raised clearly explains that. Instead of setting error.response , try to use the actual data from error.response. Eg error.response.data . Also, you can try replacing the error.response with a string and see how that behaves, then reference the string you need from error.response.data .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM