#fetching data locally

16 messages · Page 1 of 1 (latest)

full grove
#

Hello Community!

I have deployed my app in Vercel and I am struggling to fetch my data from MongoDB (which is working locally).
I really think it's something simple but after reading the docs and checking some examples I can't tell what's the issue.

The following code contains the problematic data fetch (I assume that fixing this will resolve the other case where data fetching doesn't work):
`// On mount fetch the list of all Todos from the Mongo database
useEffect(() => {
const controller = new AbortController();
const signal = controller.signal;

async function getTodos() {
  const options = {
    method: "GET",
    url: "/api/todos",
    signal,
  };

  try {
    const response = await axios.request(options);

    if (response.statusText !== "OK") {
      throw new Error("Failed to fetch Todos");
    }
    const todos = response.data.todos;
    // Set the state so the list can be displayed to the user
    setTodosArray([...todos]);
  } catch (error) {
    console.log(error);
  }
}

getTodos();

return () => {
  // Cleanup function

  controller.abort();
};

}, []);`

In Vercel it throws the "Failed to fetch Todos" error that is thrown if the response status is not OK. I also see that the Integration is properly set in Vercel and the MONGODB_URI env variable was automatically added when the setup was complete.
Thank you for the assistance in advance!

upbeat cedarBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

prisma sun
full grove
#

Yes, it gives me the proper data

prisma sun
full grove
#

It's from the app on Vercel when I used https://<my-vercel-url>/api/todos.

prisma sun
#

then it should work

#

could you share the url?

full grove
#

It even works when I run it in the console...

prisma sun
#

maybe try this instead

   if (response.status !== 200) {
    throw new Error("Failed to fetch Todos");
  }
#

i can see the data

#

statusText is blank or unsupported with http/2

#

the reason why it works on localhost is when you are fetching locally, it use http/1.1

full grove
#

Wow, can't believe it was this. It now works! Also it rendered an empty list even though I have data in the collection but it turns out that during deployment a new collection is added.
Thank you very much for the help Ray!

upbeat cedarBOT
#
✅ Success!

This question has been marked as answered! If you have any other questions, feel free to create another post

Jump to answer

[Click here](#1191567566716551208 message)