Next.js Discord

Discord Forum

Unable to fetch the content on refresh

Unanswered
Giant panda posted this in #help-forum
Open in Discord
Giant pandaOP
I have a case scenario in which I am fetching a particular content from db after logging in. It renderes as expected. However when I refresh, I couldn't see it fetching and it goes to else path of the fetch call



//file from where I am fetching the data from db
import { collection, getDocs } from 'firebase/firestore';
import { auth } from '../server/auth';
import { db } from './firebase';

export async function fetchData() {
let data: any[] = [];
const user: any = auth.currentUser;
console.log(user);
const uid:any = auth.currentUser?.uid;
console.log(uid);
if (user!=null) {
await getDocs(collection(db, uid))
.then((querySnapshot) => {
querySnapshot.forEach((doc) => {
data.push({ ...doc.data(), id: doc.id });
}); });
}else {data.push({ success: false });}
return JSON.stringify(data);
}
In the above code, on refresh, I could see user is null. but if I do any minor update and save the file, it fetches the data correctly. I am very new to nextjs and pretty sure, I am doing something wrong in here on fetching data while

0 Replies