fetching data locally
Answered
Petrov posted this in #help-forum
![Avatar](https://cdn.discordapp.com/avatars/298779559975976960/8366427fe4be689a4b89e27a4e6780ed.webp?size=256)
PetrovOP
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):
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!
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!
Answered by Ray
maybe try this instead
if (response.status !== 200) {
throw new Error("Failed to fetch Todos");
}
14 Replies
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
hi, can you try to go to
https://your-url/api/todos
on your browser and see what the result you get?![Avatar](https://cdn.discordapp.com/avatars/298779559975976960/8366427fe4be689a4b89e27a4e6780ed.webp?size=256)
PetrovOP
Yes, it gives me the proper data
![Image](https://cdn.discordapp.com/attachments/1191567566716551208/1191570255324131408/image.png?ex=65a5eb47&is=65937647&hm=cbc64015a148c551eb88f07cb0f60f578fdc42b8f74b81b7ce94c4ba8cf75163&)
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
is this from the app on vercel or locally?
![Avatar](https://cdn.discordapp.com/avatars/298779559975976960/8366427fe4be689a4b89e27a4e6780ed.webp?size=256)
PetrovOP
It's from the app on Vercel when I used https://<my-vercel-url>/api/todos.
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
then it should work
could you share the url?
It even works when I run it in the console...
![Image](https://cdn.discordapp.com/attachments/1191567566716551208/1191573749338734672/image.png?ex=65a5ee88&is=65937988&hm=6a18e6055416318ffc3a77e9e428a31301c16e6b143b9130fac7c341227f2aa9&)
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
maybe try this instead
if (response.status !== 200) {
throw new Error("Failed to fetch Todos");
}
Answer
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
i can see the data
![Image](https://cdn.discordapp.com/attachments/1191567566716551208/1191574095607906354/image.png?ex=65a5eeda&is=659379da&hm=bae9e0b9c189908ad014ca1fdda736b4c5ac4351e996e3c8a97c689407003382&)
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
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
![Avatar](https://cdn.discordapp.com/avatars/298779559975976960/8366427fe4be689a4b89e27a4e6780ed.webp?size=256)
PetrovOP
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!
Thank you very much for the help Ray!
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
np