el10savio / TODO-Fullstack-App-Go-Gin-Postgres-React

A fullstack Todo List built using Go/Gin/Postgres/React stack deployed using docker-compose

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Getting 'Unhandled Rejection (TypeError): Failed to fetch' error

shubham-aaic opened this issue · comments

When I try to hit homepage I get 'Unhandled Rejection (TypeError): Failed to fetch' error. Please help me out.
Screenshot from 2022-05-27 12-43-10

Hi @shubham-aaic,

Can you please make sure that you have the Go server & Postgres running. Looks like the above error is because it is unable to get a response back from the Go API server.

In the project root, you can use docker-compose to build the project

docker-compose build
docker-compose up

After running the above two commands you should see an output similar to the one below for docker-compose ps which means that all the services are up.

$ docker-compose ps
                        Name                                      Command               State            Ports         
-----------------------------------------------------------------------------------------------------------------------
todo-fullstack-app-go-gin-postgres-react_go-api_1      /go/bin/build ./wait-for-i ...   Up      0.0.0.0:8081->8081/tcp 
todo-fullstack-app-go-gin-postgres-react_postgres_1    docker-entrypoint.sh postgres    Up      0.0.0.0:50324->5432/tcp
todo-fullstack-app-go-gin-postgres-react_react-app_1   docker-entrypoint.sh npm start   Up      0.0.0.0:3000->3000/tcp 

Yes, go api server and postgres server are running properly you can refer below image. When I hit the api server locally it gives me response back. But it doesn't work in case of react.
Screenshot from 2022-05-28 12-29-33
Screenshot from 2022-05-28 12-27-55
Screenshot from 2022-05-28 12-44-16

Alright, looks like we need to debug it out.

Can you add the following line to componentDidMount() in frontend/src/components/todolist.js and restart the docker-compose setup, after that can you please send the console log output once you open the page http://localhost:3000/
and a snapshot of the docker-compose logs if the error pops up again.

componentDidMount() {
    fetch("http://localhost:8081/items")
      .then(res => res.json())
      .then(json => this.setState({ items: json.items }))
      .catch(err => console.log(err)); 
  }

I tried the code.Refer below screenshots!
Screenshot from 2022-05-28 19-04-48

Screenshot from 2022-05-28 19-30-08
Screenshot from 2022-05-28 20-08-06

btw it says CORS error. I am running this application on ec2-instance!
I got the reason for this error it is a CORS error because when I try to hit the application locally using curl localhost:3000 it returns 200 success code but it doesn't work when accessed from browser. I hope you got the idea now.
image