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:)

itsame
  • 173
  • 1
  • 9

2 Answers2

2

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);
      });
  };
SuleymanSah
  • 13,055
  • 5
  • 17
  • 42
  • @hallowed that's a cors problem, you need to enable cors on your api, there are lots of answers about cors here at stackoverflow. – SuleymanSah Apr 03 '20 at 12:38
  • 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 http://localhost:5000... oof – itsame Apr 03 '20 at 12:41
  • @hallowed it is a new problem about cors. Check this question and answer https://stackoverflow.com/questions/48728334/fetch-api-cannot-load-file-c-users-jack-desktop-books-h-book-site-public-api?noredirect=1&lq=1 – SuleymanSah Apr 03 '20 at 12:42
  • It's not really a problem. You just have to serve your files from some kind of webserver. Thanks for your help! – itsame Apr 03 '20 at 12:44
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
      })
    });
  };
Marcos Mitozo
  • 91
  • 1
  • 3