Next.js tutorial database error

// I add the import …
import { fetchRevenue } from ‘@/app/lib/data’;

// No problem
// Add the await fetchRevenue
const revenue = await fetchRevenue();

// Error
26 | console.error(‘Database Error:’, error);

27 | throw new Error(‘Failed to fetch revenue data.’);

// Database has all tables and sample data. RLS is disabled.
// Googled and found similar errors but not this one. :=(
// Thanks for you time and interest.

Hi, @ckashby! Thanks for your patience :pray:

First, let’s check the implementation of the fetchRevenue function. It should be located in the @/app/lib/data.ts file. Can you please confirm that the function looks similar to this?

export async function fetchRevenue() {
  try {
    // SQL query to fetch revenue data
    const data = await sql<Revenue>`SELECT * FROM revenue`;
    
    return data.rows;
  } catch (error) {
    console.error('Database Error:', error);
    throw new Error('Failed to fetch revenue data.');
  }
}

If this is correct, the error you’re seeing suggests that there’s an issue with the database connection or the SQL query execution. Here are a few things we can check:

  1. Database Connection: is it correctly set up in your environment variables?
  2. SQL Query: Make sure the ‘revenue’ table exists in your database and has the correct structure.
  3. Error Handling: Let’s modify the error handling to get more information about what’s going wrong.

Let me know how you get on!

1 Like





Hello!
I have the same problem while going through the tutorial.

I compared two approaches:

  1. Using @vercel/postgres (attached a screenshot)
  2. Using postgres (attached a screenshot)

In the first case, that is, when using @vercel/postgres, an error 500 is issued (attached a screenshot)

In the second case, that is, when using postgres, data is retrieved from the database without problems (attached a screenshot)

That is, the problem is really on the side of @vercel/postgres, and not on the side of the database.

Please help me figure out this problem.
I would like to use import { sql } from ‘@vercel/postgres’;.

And does @vercel/postgres really allow you to protect the site from SQL injections? If so, then I would really, really like you to help with the problem described above.

With respect to you!

@pawlean

Here Docs: When I test under step seven "Fetching data for the dashboard overview page" I get the error "Error: Failed to fetch revenue data." · Issue #947 · vercel/next-learn · GitHub
the problem is not resolved

What wrong with sql from @vercel/postgres?

With respect

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