javascript – Errors in fetching Data on MongoDB


Hello ive been having an issue with my webApp . My homepage cant fetch data from the backend . ive have a js file at the backend name studentRecords that populate details from the data models but when i try login with my admission number . The response is that it has fail to fetch the student records. Any assistance in this will be highly appreciated .
Here is the code for studentRecords.

router.get('/:admissionNumber', async (req, res) => {
res.json({ message: 'This route is accessible.' });
   try {
       const { admissionNumber } = req.params;


const credentials = await Record.findOne({ admissionNumber }).populate('studentRecord');

if (!credentials || !credentials.studentRecord) {
  return res.status(404).json({ message: 'Student data is not 
  available.' });
}

res.json({ student: credentials.StudentModel});
} catch (error) {
console.error('Error fetching student data:', error);
res.status(500).json({ message: 'Failed to load student data.' 
  });
   }
  });

 module.exports = router;

I tried debugging the studentRecords.js file but ive not been able to solve the issue . And what i expected is that after i logged in with my admission number at the login page , I’m able to see the student records at the home component . But it has hasn’t work.

and here is the code from my frontend .

const fetchStudentData = useCallback(async () => {
try {
  const response = await 
axios.get(`https://localhost:3000/api/student/${admissionNumber}`);
  alert(response.data.student);

  if (response.data && response.data.student) {
    const { student } = response.data;
    console.log('Student Data:', student);

    setStudentName(student.studentName);
    setFeeBalance(student.feeBalance);
    setFeeStatement(student.feeStatement);
    setCourse(student.course);
    setAdmissionNumber(student.admissionNumber);
  } else {
    alert("Ooops there is an eerror here");
    setError('Student data is not available.');
    setShowError(true);
    setTimeout(() => setShowError(false), 3000);
  }
} catch (error) {
  console.error('Error fetching student data:', error);
  alert(error);
  setError('Failed to load student data.');
  setShowError(true);
  setTimeout(() => setShowError(false), 3000);
}

Leave a Reply

Your email address will not be published. Required fields are marked *