Next.js Discord

Discord Forum

Error: Cannot destructure property 'tickets' of '(intermediate value)' as it is undefined.

Unanswered
Transvaal lion posted this in #help-forum
Open in Discord
Transvaal lionOP
Hey, i am following a turtorial and i keep getting this error
Error: Cannot destructure property 'tickets' of '(intermediate value)' as it is undefined.
const getTickets = async () => {
  try {
    const res = await fetch("http://localhost:300/api/Tickets", {
      cache: "no-store",
    });
    console.log("test", res.json());
    return res.json();
  } catch (error) {
    console.log("Failed to get tickets", error);
  }
};

const Dashboard = async () => {
  const { tickets } = await getTickets();
};

I think maybe it has something to do with my GET since in console there is also this error (GET http://localhost:3000/ 500 (Internal Server Error))
export async function GET() {
  try {
    const tickets = await Ticket.find();
    return NextResponse.json({ tickets }, { status: 200 });
  } catch (error) {
    console.log("here");
    return NextResponse.json({ message: "Error", error }, { status: 500 });
  }
}

19 Replies

Spectacled bear
You are missing await in while returning the response from getTickets. Change this line to

await return res.json();
Spectacled bear
What's the latest code now and error?
Transvaal lionOP
same error code the same with the await
Spectacled bear
Try this


const response = await getTickets();
if (response) {
const { tickets } = response
}
Transvaal lionOP
Error: tickets is not defined
Seems the get isnt getting anything?
@Spectacled bear Try this > const response = await getTickets(); > if (response) { > const { tickets } = response > }
Transvaal lionOP
Failed to get tickets TypeError: fetch failed
    at node:internal/deps/undici/undici:12502:13
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async getTickets (webpack-internal:///(rsc)/./app/page.jsx:12:21)
    at async Dashboard (webpack-internal:///(rsc)/./app/page.jsx:22:25) {
  [cause]: AggregateError [ECONNREFUSED]:
      at internalConnectMultiple (node:net:1117:18)
      at afterConnectMultiple (node:net:1684:7)
      at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17) {
    code: 'ECONNREFUSED',
    [errors]: [ [Error], [Error] ]
  }
}
@Spectacled bear Are you sure you are connected to DB?
Transvaal lionOP
It worked before
I shut down my pc reopened the project and from that point it didnt work
Spectacled bear
Check if you are connected
Transvaal lionOP
How?
@Transvaal lion How?
Spectacled bear
Adding a console statement or try to do any other operation like fetch data from any other collection/tablea
@Spectacled bear Adding a console statement or try to do any other operation like fetch data from any other collection/tablea
Transvaal lionOP
import mongoose, { Schema } from "mongoose";

mongoose.connect(process.env.MONGODB_URI);
mongoose.Promise = global.Promise;
const Ticket = mongoose.models.Ticket || mongoose.model("Ticket", ticketSchema);
export default Ticket;
@Transvaal lion js import mongoose, { Schema } from "mongoose"; mongoose.connect(process.env.MONGODB_URI); mongoose.Promise = global.Promise;
Spectacled bear
Try fetching data for any other table, or try to do any other db operation.
Spectacled bear
Hi, is it fixed?