Currently, I am utilizing the getServerSideProps
function to retrieve data from my Firebase database for my Next.js application.
This is how my code snippet appears:
export async function getServerSideProps(context) {
const session = await getSession(context);
const products = await fetch("https://database-73695.firebaseio.com/").then(
(res) => res.json()
);
return {
props: {
products,
session
},
};
}
However, I encountered an error message saying:
"FetchError: invalid json response body at https://database-73695.firebaseio.com/ reason: Unexpected token F in JSON at position 0"
Some users have reported this error occurring when the fetched data is text rather than an object. I attempted to resolve this by changing the response from res.json
to res.text
, but then encountered an error stating that "text is undefined"
.
Does anyone have any insights on what might be causing this issue?
UPDATE: After experimenting with different fetching methods, I came across the error:
Firebase error. Please ensure that you have the URL of your Firebase Realtime Database instance configured correctly.
All fetching codes (with or without using getServerSideProps) function properly when connected to other APIs. The URL for my database originates from Firestore and is structured as follows:
It is important to note that the database is hosted in us-central region. Another point worth mentioning is that the database already contains a collection called "users" which is linked to Stripe transactions and functions smoothly.
If anyone has any suggestions or ideas, they would be greatly appreciated. Thank you for taking the time to assist.