0

I'm trying to build a Login / Register form with react and node / express / (mongo) as my backend. The backend works fine. When I send a POST request (with postman) to /register, everything works fine and the credentials are stored in the DB.

I tried to implement the form now with react on the client-side, but when I try to POST I always get the error: Unhandled Rejection (TypeError): Failed to fetch.

This is my code:

import React, { Component } from 'react';

class Register extends Component {
  state = { email: '', password: '' };

  handleInputChange = event => {
    const { value, name } = event.target;
    this.setState({
      [name]: value
    });
  };

  onSubmit = event => {
    event.preventDefault();
    fetch('localhost:3000/register', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        email: this.state.email,
        password: this.state.password
      })
    });
  };

  render() {
    return (
      <form onSubmit={this.onSubmit} noValidate>
        <h1>Login</h1>
        <input
          type="email"
          name="email"
          placeholder="Enter email"
          value={this.state.email}
          onChange={this.handleInputChange}
        />
        <input
          type="password"
          name="password"
          placeholder="Enter password"
          value={this.state.password}
          onChange={this.handleInputChange}
        />
        <input type="submit" value="Submit" />
      </form>
    );
  }
}

export default Register;

Help would be appreciated:)

2 Answers 2

3

You need to use then block to actually call your api.

  onSubmit = event => {
    event.preventDefault();
    fetch("localhost:3000/register", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({
        email: this.state.email,
        password: this.state.password
      })
    })
      .then(res => {
        console.log("response: ", res);
      })
      .catch(err => {
        console.log("error:", err);
      });
  };
Sign up to request clarification or add additional context in comments.

4 Comments

@hallowed that's a cors problem, you need to enable cors on your api, there are lots of answers about cors here at stackoverflow.
I accidentally deleted my comment... For everyone who may be reading this, the comment was: "I'm now getting: Fetch API cannot load localhost:5000/register. URL scheme must be "http" or "https" for CORS request." I already enabled cors. The problem was, that I wrote localhost:5000 instead of localhost:5000... oof
@hallowed it is a new problem about cors. Check this question and answer stackoverflow.com/questions/48728334/…
It's not really a problem. You just have to serve your files from some kind of webserver. Thanks for your help!
0

In my case, inserting "http://" before "localhost" worked!

onSubmit = event => {
    event.preventDefault();
    fetch('http://localhost:3000/register', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        email: this.state.email,
        password: this.state.password
      })
    });
  };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.