T
TanStack6mo ago
ambitious-aqua

Dynamic pages from slugs in CMS

Hi, i have a problem that my slug isnt passed to the fetchSlugs1 server function. I am new to Tanstack and i cannot wrap my head around this problem. Thank you for any help.
No description
3 Replies
ambitious-aqua
ambitious-aquaOP6mo ago
my bad, i found a documentation, thanks anyway.
No description
fair-rose
fair-rose6mo ago
I think your validator is wrong, try this:
type Data = {
slug: string;
};

export const someAction = createServerFn({ method: "GET" })
.validator((data: unknown): Data => {
if (typeof data !== "object" || data === null) {
throw new Error("data must be an object");
}

if ("slug" in data && typeof data.slug !== "string") {
throw new Error("Data.slug must be a string");
}

return data as Data;
})
.handler(async ({ context, data }) => {
console.log(data.slug)
});
type Data = {
slug: string;
};

export const someAction = createServerFn({ method: "GET" })
.validator((data: unknown): Data => {
if (typeof data !== "object" || data === null) {
throw new Error("data must be an object");
}

if ("slug" in data && typeof data.slug !== "string") {
throw new Error("Data.slug must be a string");
}

return data as Data;
})
.handler(async ({ context, data }) => {
console.log(data.slug)
});
If I were you I would probably use zod (validator library) for this.
export const dcreateTagAction = createServerFn({ method: "POST" })
.validator(z.object({slug:z.string()}))
.handler(async ({ context, data }) => {
console.log(data.slug)
});
export const dcreateTagAction = createServerFn({ method: "POST" })
.validator(z.object({slug:z.string()}))
.handler(async ({ context, data }) => {
console.log(data.slug)
});
ambitious-aqua
ambitious-aquaOP6mo ago
i will check it out, many thanks!

Did you find this page helpful?