SyntaxError: Unexpected token < in JSON at position 0
Answered
Alligator mississippiensis posted this in #help-forum
Alligator mississippiensisOP
2nd Error:
Error: Failed to collect page data for /channels/[id]Answered by Ray
export async function generateStaticParams() {
/**
* need to include 'NEXTAUTH_URL` env variable here as it is a server component
*/
const data: TChannel[] = await Channels.find({})
return data.map((channel) => ({
id: channel._id.toString()
}));
}20 Replies
Alligator mississippiensisOP
when I execute
npm run build locally, I don't get anything "fancy"it runs smoothly
can you show the code in /channels/[id]/page.tsx
Alligator mississippiensisOP
import ChannelView from "@/components/client/ChannelView";
import { TChannel } from "@/components/client/ChannelsView";
import Channels from "@/models/Channels";
import connect from "@/utils/server-helper";
import { Metadata } from "next";
import { FC } from "react";
export async function generateMetadata({params}: {params: {id:string}}) {
await connect()
const data: TChannel | null = await Channels.findById(params.id);
return {
title: `${data?.chnl_name}`,
description: `${data?.chnl_desc}`,
creator: `${data?.createdBy}`,
keywords: `${data?.category}`,
} as Metadata
}
interface PageProps {
params:{
id: string
}
}
export async function generateStaticParams() {
/**
* need to include 'NEXTAUTH_URL` env variable here as it is a server component
*/
const res = await fetch(`${process.env.NEXTAUTH_URL}/api/channels`);
const data: TChannel[] = await res.json();
return data.map((channel) => ({
id: channel._id
}));
}
const Page: FC<PageProps> = ({params}) => {
return (
<main className="pt-4 w-4/5 min-h-screen">
<section className="flex flex-col mt-15">
<ChannelView params={params}/>
</section>
</main>
);
};
export default Page;@Alligator mississippiensis tsx
import ChannelView from "@/components/client/ChannelView";
import { TChannel } from "@/components/client/ChannelsView";
import Channels from "@/models/Channels";
import connect from "@/utils/server-helper";
import { Metadata } from "next";
import { FC } from "react";
export async function generateMetadata({params}: {params: {id:string}}) {
await connect()
const data: TChannel | null = await Channels.findById(params.id);
return {
title: `${data?.chnl_name}`,
description: `${data?.chnl_desc}`,
creator: `${data?.createdBy}`,
keywords: `${data?.category}`,
} as Metadata
}
interface PageProps {
params:{
id: string
}
}
export async function generateStaticParams() {
/**
* need to include 'NEXTAUTH_URL` env variable here as it is a server component
*/
const res = await fetch(`${process.env.NEXTAUTH_URL}/api/channels`);
const data: TChannel[] = await res.json();
return data.map((channel) => ({
id: channel._id
}));
}
const Page: FC<PageProps> = ({params}) => {
return (
<main className="pt-4 w-4/5 min-h-screen">
<section className="flex flex-col mt-15">
<ChannelView params={params}/>
</section>
</main>
);
};
export default Page;
export async function generateStaticParams() {
/**
* need to include 'NEXTAUTH_URL` env variable here as it is a server component
*/
const data: TChannel[] = await Channels.find({})
return data.map((channel) => ({
id: channel._id
}));
}Alligator mississippiensisOP
I am using mongodb
mongoose
sorry
const data: TChannel[] = await Channels.find({}) should work?Alligator mississippiensisOP
console.log(data)
Alligator mississippiensisOP
inside generateStaticParams ?
yes
after Channels.find()
Alligator mississippiensisOP
export type TChannel = {
_id: string;
chnl_id: string;
createdBy: string;
chnl_name: string;
chnl_desc: string;
category: string;
createdAt: Date;
updatedAt: Date;
};Original message was deleted
export async function generateStaticParams() {
/**
* need to include 'NEXTAUTH_URL` env variable here as it is a server component
*/
const data: TChannel[] = await Channels.find({})
return data.map((channel) => ({
id: channel._id.toString()
}));
}Answer
Alligator mississippiensisOP
problem is resolved 🥳
thanks again @Ray
np