octo3.
octo3.5mo ago

not found ctx.data

The data set in the handler is not reflected, is there any reason for this?
import { FreshContext, Handlers } from "$fresh/server.ts";

export const handler: Handlers = {
GET: async (_req: Request, ctx: FreshContext) => {
return await ctx.render({
message: "Welcome to Fresh"
});
},
};

export default function Home(_req: Request, ctx:FreshContext) {
console.log(ctx.data) // undefined !!!
return (
<div >
{ctx.data.message}
</div>
);
}
import { FreshContext, Handlers } from "$fresh/server.ts";

export const handler: Handlers = {
GET: async (_req: Request, ctx: FreshContext) => {
return await ctx.render({
message: "Welcome to Fresh"
});
},
};

export default function Home(_req: Request, ctx:FreshContext) {
console.log(ctx.data) // undefined !!!
return (
<div >
{ctx.data.message}
</div>
);
}
2 Replies
Deno AI Helper
Deno AI Helper5mo ago
I wasn't able to find the answer to your question in the official Deno documentation. However, based on the code snippet you've provided, it seems that you are trying to access ctx.data in your default function Home, but ctx.data is not being set in the GET handler. In the GET handler, you are passing an object with a message property to ctx.render(), but this does not automatically set ctx.data. The ctx.render() function is used to render a page with the given data, but it does not modify the ctx object itself. If you want to pass data to the Home component, you should ensure that the data is being set on the context object properly, which is typically done within the handler functions. However, without further documentation or context, I cannot provide a definitive solution. Please note that this advice is not based on the Deno documentation, and I would advise you to double-check the accuracy of this advice. It's possible that there might be a specific way to handle context data in Fresh that is not covered here.
MitchMc
MitchMc5mo ago
Routes | Fresh docs
Routes are the basic building block of Fresh applications. They are used to define the behaviour the application when a given path is requested.