Unexpected end of JSON input error in MVC architecture exercises

Hello I am currently in the fullstack path and I am in the mvc architecture section. Here is a link to the project: https://www.codecademy.com/article/mvc-architecture-for-full-stack-app

I finished the tutorial but when I run the application I get this error:

Here is my code where the error happens:

export const fetchExpenses = async (date) => {
  const selectDate = new Date(date).getTime() || new Date().getTime();
  const res = await fetch(`/api/expense/list/${selectDate}`);

   return res.json();
  
};

It seems the return res.json() is causing the error but I am not sure why. I checked the solution code and it is exactly the same way they have it. Any ideas why this is happening? Any help is appreciated. Thank you and happy coding :slight_smile:

Hi codejumper, I know your question was a while ago but maybe this will help someone else. I had the same issue. The problem is that the provided starter code has a bug in the controllers/index.js file, in the exports.expenseByDate function starting at line 69. If you replace the relevant lines in the starting code with the relevant code snippet from the solution code, this should fix the problem. Note there is also a typo which you’ll also need to fix - params not parmas on line 70!

Here’s the correct code:

exports.expenseByDate = async (req, res, next) => {
  const expenseDate = Number(req.params.expenseDate);
  try {
    const expenseQuery = await pool.query(
      'SELECT * FROM expenses WHERE created_at BETWEEN $1 AND $2',
      [
        startOfDay(new Date(expenseDate)).toISOString(),
        endOfDay(new Date(expenseDate)).toISOString(),
      ]
    );

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.