0

I have a Spring Boot application running on the backend on localhost:8080. The front end is react app on localhost:3000. I'm trying to call a url from the backend but result is empty. Why?

If I call the backend url directly in a browser like this:

http://localhost:8080/appointments

I see the data come in ok (in the form of a view template) that looks like this: enter image description here

But when I call the same url from my react app like this:

    useEffect(() => {
    (
        async () => {
            const result = await axios("http://localhost:8080/appointments");
            console.log("result="+result);
            //setData(result.data);
        }
    )();
}, 

then the console shows and empty result:

result=[object Object]

Sorry if this is a basic question. I'm new to react.

Samim Hakimi
  • 705
  • 8
  • 22
user3217883
  • 1,216
  • 4
  • 38
  • 65

1 Answers1

1

You forgot to mention get request in axios.

Try this way:

    useEffect(() => {
    (
        async () => {
            const result = await axios.get("http://localhost:8080/appointments");
            console.log("result =", result);
            //setData(result.data);
        }
    )();
}, 

Also, when you console.log("result="+result); you are concatenating an object to string, instead use console.log("result =", result);

Samim Hakimi
  • 705
  • 8
  • 22
  • I think I knew that once and forgot. Thank you! Unfortunately, it isn't returning what I was expecting. It is returning the entire html table, not just the data. I'll need to dig deeper. – user3217883 Apr 27 '20 at 23:01