Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data passed with ctx.render to async route is undefined #2435

Closed
imblowfish opened this issue May 18, 2024 · 1 comment
Closed

Data passed with ctx.render to async route is undefined #2435

imblowfish opened this issue May 18, 2024 · 1 comment

Comments

@imblowfish
Copy link

Fresh version 1.6.8
Deno version 1.43.5

Hello, I tried to do the same thing as in this blog https://deno.com/blog/setup-auth-with-fresh

import type { Handlers, PageProps } from "$fresh/server.ts";
import { getCookies } from "std/http/cookie.ts";

interface Data {
  isAllowed: boolean;
}

export const handler: Handlers = {
  GET(req, ctx) {
    const cookies = getCookies(req.headers);
    return ctx.render!({ isAllowed: cookies.auth === "bar" });
  },
};

export default function Home({ data }: PageProps<Data>) {
  return (
    <div>
      <div>
        You currently {data.isAllowed ? "are" : "are not"} logged in.
      </div>
    </div>
  );
}

And my code looks like this

interface AuthData {
  isAllowed: boolean;
}

export const handler: Handlers = {
  GET(req, ctx) {
    const cookies = getCookies(req.headers);
    return ctx.render({
      isAllowed: cookies.auth === "bar"
    } satisfies AuthData);
  },
};

export default async function Home(props: PageProps<AuthData>) {
   console.log(props.data);
   ...
}

And in the result I see that props.data is undefined

But if I'll remove async from the Home like this:

interface AuthData {
  isAllowed: boolean;
}

export const handler: Handlers = {
  GET(req, ctx) {
    const cookies = getCookies(req.headers);
    return ctx.render({
      isAllowed: cookies.auth === "bar"
    } satisfies AuthData);
  },
};

export default function Home(props: PageProps<AuthData>) {
   console.log(props.data);
   ...
}

then I can see { isAllowed: false } as expected

@imblowfish
Copy link
Author

Whoops, my bad
Found in the documentation https://fresh.deno.dev/docs/concepts/routes that route parameters should be rewritten to

export default async function Home(req: Request, ctx: RouteContext) {
  console.log(ctx.data);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant