I’m new to next.js and making this project following an youtuber, here you can find my code: https://github.com/Fanguiee/ticketing-app/tree/edit-existing-item
or you can also read below.Thanks in advance:)
Screenshot:
enter image description here
Error in text:
⨯ appTicketPage[id]page.jsx (30:40) @ foundTicket
⨯ TypeError: Cannot read properties of undefined (reading 'foundTicket')
at TicketPage (./app/TicketPage/[id]/page.jsx:35:45)
28 | } else {
29 | updateTicketData = await getTicketById(params.id);
> 30 | updateTicketData = updateTicketData.foundTicket;
| ^
31 | }
32 | return <TicketForm ticket={updateTicketData} />;
33 | };
appTicketPage[id]page.jsx:
import TicketForm from "@/app/(components)/TicketForm";
const getTicketById = async (id) => {
try {
const res = await fetch(`http://localhost:3000/api/Tickets/${id}`, {
method: "GET",
cache: "no-store",
});
if (!res.ok) {
throw new Error(`Failed to fetch Ticket by id ${id} .`);
}
console.log("Ah!");
console.log(res);
return res.json();
} catch (error) {
console.log(error);
}
};
const TicketPage = async ({ params }) => {
const EDITMODE = params.id === "new" ? false : true; //Tickecpage/new or TicketPage/123?
let updateTicketData = {};
if (!EDITMODE) {
updateTicketData = {
_id: "new",
};
} else {
updateTicketData = await getTicketById(params.id);
updateTicketData = updateTicketData.foundTicket;
}
return <TicketForm ticket={updateTicketData} />;
};
export default TicketPage;
appapiTickets[id]route.js:
import TicketForm from "@/app/(components)/TicketForm";
const getTicketById = async (id) => {
try {
const res = await fetch(`http://localhost:3000/api/Tickets/${id}`, {
method: "GET",
cache: "no-store",
});
if (!res.ok) {
throw new Error(`Failed to fetch Ticket by id ${id} .`);
}
console.log("Ah!");
console.log(res);
return res.json();
} catch (error) {
console.log(error);
}
};
const TicketPage = async ({ params }) => {
const EDITMODE = params.id === "new" ? false : true; //Tickecpage/new or TicketPage/123?
let updateTicketData = {};
if (!EDITMODE) {
updateTicketData = {
_id: "new",
};
} else {
updateTicketData = await getTicketById(params.id);
updateTicketData = updateTicketData.foundTicket;
}
return <TicketForm ticket={updateTicketData} />;
};
export default TicketPage;
i have node.js :v18.16.0, have problem updating it:( maybe it is the problem?
package.json:
{
"name": "ticketing-app",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"@fortawesome/fontawesome-svg-core": "^6.4.2",
"@fortawesome/free-solid-svg-icons": "^6.4.2",
"@fortawesome/react-fontawesome": "^0.2.0",
"autoprefixer": "10.4.16",
"eslint": "8.52.0",
"eslint-config-next": "^14.0.0",
"mongodb": "^6.2.0",
"mongoose": "^7.6.3",
"next": "^13.0.8",
"postcss": "8.4.31",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"tailwindcss": "3.3.5"
}
}
First i used next.js 14.0.0 and then tried older version after reading the post TypeError: Cannot read properties of undefined (reading 'call') on Next.js
(next.js 13.0.8 in package.json(but somehow the actual version is Next.js v13.5.6).The Youtuber from whom i learned this project used "next": "13.4.13",should i try that version maybe??)
i also tried npm install next@canary
, but it didnt help.
2
Answers
you can use optional chaining to make sure the file you fetched by ID exists before accessing the attribute:
this should put
undefined
insideupdateTicketData
ifupdateTicketData
doesn’t existsI assume that everything in the ‘getTicketById’ function works fine. I believe the issue is that we can’t pass the data from RSC to the component without it being in JSON format. You can try the following:
Hope, it works.