XoutDragon
XoutDragon10mo ago

Metadata + Convex

Is it possible to retrive data from convex database to use in a dynamic metadata?
4 Replies
XoutDragon
XoutDragonOP10mo ago
I ran into a problem of useQuery hook not being able to be used in a non react function like
export async function generateMetadata({ params }: DocumentIdPageProps) {
// eslint-disable-next-line react-hooks/rules-of-hooks
const document = useQuery(api.documents.getById, {
documentId: params.documentId,
});

if (document === undefined) {
return {
title: 'Loading...',
description: 'Loading...',
};
}

if (document === null) {
return {
title: 'Not found',
description: 'Not found',
};
}

return {
title: document.title,
};
}
export async function generateMetadata({ params }: DocumentIdPageProps) {
// eslint-disable-next-line react-hooks/rules-of-hooks
const document = useQuery(api.documents.getById, {
documentId: params.documentId,
});

if (document === undefined) {
return {
title: 'Loading...',
description: 'Loading...',
};
}

if (document === null) {
return {
title: 'Not found',
description: 'Not found',
};
}

return {
title: document.title,
};
}
Is there a way to get data from convex without having to use a hook that can be used in outside non react functions? (without having to use an external backend) Thanks for any help!
lee
lee10mo ago
Try this in a function called within a react app
const convexClient = useConvex();
const result = await convexClient.query(api.file.function, { args });
const convexClient = useConvex();
const result = await convexClient.query(api.file.function, { args });
Or if you're not in React at all, you can use new ConvexHttpClient
Michal Srb
Michal Srb10mo ago
Or fetchQuery if you're in Next.js (even if you're not in Next.js you can use fetchQuery and override the url)
XoutDragon
XoutDragonOP10mo ago
Tyvm

Did you find this page helpful?