0

Why does this log an empty array [] (not supposed to be empty) in my console even though i get results via my API?

node js code:

app.get("/todos", async (req, res) => {
    const todos = await todo.find()
    res.json(todos)
})

react code:

  const [todos, setTodos] = useState([])

  useEffect(() => {
    GetTodos()  

    console.log(todos); 
  }, [])

  const GetTodos = () => {
        fetch(API_BASE + "/todos")
            .then(res => res.json())
            .then(data => setTodos(data))
            .catch((err) => console.error("Error: ", err));
    }

All my endpoints seem to work with postman, i just can't figure out why react does't spit out the same result

chris z
  • 5
  • 2

1 Answers1

0

fixed it. the problem was that i logged to the console in the useEffect hook and not in the GetTodos() function, so it just returned the initial useState value, don't know why though

chris z
  • 5
  • 2