skip to Main Content

The call is successful but dosent return anything.

Server:

export const getTopMovies = async (req, res) => {
console.log("Inside Server")
res.status = 200
res.data = "movies";
return res;
> }

Client:

import { useEffect } from 'react';


const topMovies = () => {

    //const [movies, setMovies] = useState();

    useEffect(() => {
      fetch('http://localhost:8000/movies/getTopMovies')
      .then(response => response.json())
      .then(data => console.log(data.data))
    }, [])
    
    return (
      <div className="max-h-screen w-full fixed h-screen bg-BGColor pt-6">
        <h1 className="text-zinc-50 pt-4 pl-10 text-2xl ">Top Movies</h1>
        <div className="min-w-[82.5%] absolute min-h-[78%] mt-6 ml-10 rounded-3xl bg-BGColorLight1 text-white"> 
        </div>
      </div>
    )
    }
    
  export default topMovies

Log inside server

I tried to return a simple string and log the response but didn’t work.

2

Answers


  1. You should return the response.json()

    useEffect(() => {
          fetch('http://localhost:8000/movies/getTopMovies')
          .then(response => { return response.json() })
          .then(data => console.log(data.data))
        }, [])
    
    Login or Signup to reply.
  2. Your getTopMovies controller doesn’t respond with anything.
    You should use the methods from the res object:

    1. Call the status method with 200 as the argument.
    2. Call the json method to send the data: "movies".

    Modify your getTopMovies into:

    export const getTopMovies = async (req, res) => {
      console.log("Inside Server");
      res.status(200).json({ data: "movies" });
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search