Skip to content

Docs: When I test under step seven "Fetching data for the dashboard overview page" I get the error "Error: Failed to fetch revenue data." #947

Open
@fatih-keon

Description

@fatih-keon

What is the documentation issue?

The error comes from the fetchRevenue() function located in the app/lib/data.ts file.

export async function fetchRevenue() {
  try {
    // Artificially delay a response for demo purposes.
    // Don't do this in production :)

    // console.log('Fetching revenue data...');
    // await new Promise((resolve) => setTimeout(resolve, 3000));

    const data = await sql<Revenue>`SELECT * FROM revenue`;

    //console.log('Data fetch completed after 3 seconds.');

    return data.rows;
  } catch (error) {
    console.error('Database Error:', error);
    throw new Error('Failed to fetch revenue data.');
  }
}

For the solution, I wanted to use the command we used in the database test before.

import { db } from "@vercel/postgres";

 const client = await db.connect();

 async function listInvoices() {
 	const data = await client.sql`
     SELECT invoices.amount, customers.name
     FROM invoices
     JOIN customers ON invoices.customer_id = customers.id
     WHERE invoices.amount = 666;
   `;

 	return data.rows;
 }

export async function GET() {
   try {
   	return Response.json(await listInvoices());
   } catch (error) {
   	return Response.json({ error }, { status: 500 });
   }
}

And with my novice coding knowledge I wrote this and ran the graph but I didn't understand why the library(import { sql } from '@vercel/postgres';
) wasn't working

import { db } from "@vercel/postgres";

const testr = await db.connect();
export async function fetchRevenue() {
  try {
    const data = await testr.sql<Revenue>`
  SELECT * FROM revenue
  `;
    return data.rows;
  } catch (error) {
    console.error('database error', error);
    throw new Error('revenue datasını çekemedi')
  }
}

Is there any context that might help us understand?

I think I'm missing something

Does the docs page already exist? Please link to it.

https://nextjs.org/learn/dashboard-app/fetching-data

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions