[id].js doesn't work
Answered
i_lost_to_loba_kreygasm posted this in #help-forum
//page.js
import Link from "next/link";
export default async function Page(){
let res =await fetch('https://jsonplaceholder.typicode.com/users');
let users =await res.json();
console.log(users)
return <ul>
{users.map(user => (
<li key={user.id}>
<Link href={`/users/${user.id}`}>
<p>See User {user.id}</p>
</Link>
</li>
))}
</ul>
}Answered by B33fb0n3
in this case you need to change your folder structure a bit. Instead of having a
[id].js you will need to create a /[id]/page.tsx. Inside the page.tsxyou can get the id by getting the params like:export default function Page({ params }) {
return <div>My Post: {params.id}</div>
}36 Replies
@B33fb0n3 hi
// [id].js
export default function UserID(){
return <h1>This</h1>
}@i_lost_to_loba_kreygasm <@301376057326567425> hi
hi, you are pointing to this message right?: https://nextjs-forum.com/post/1269205576303378453#message-1269213822166630484
@i_lost_to_loba_kreygasm yes
in this case you need to change your folder structure a bit. Instead of having a
[id].js you will need to create a /[id]/page.tsx. Inside the page.tsxyou can get the id by getting the params like:export default function Page({ params }) {
return <div>My Post: {params.id}</div>
}Answer
you can read about it [here](https://nextjs.org/docs/app/building-your-application/routing/dynamic-routes)
Now it works , thank you very much . Also why doesn't console.log work when i try
Also why doesn't console.log work when i trycan you clarify?
import Link from "next/link";
export default async function Page(){
let res =await fetch('https://jsonplaceholder.typicode.com/users');
let users =await res.json();
console.log(users) like my console in dev tools doesnt show any response@i_lost_to_loba_kreygasm js
import Link from "next/link";
export default async function Page(){
let res =await fetch('https://jsonplaceholder.typicode.com/users');
let users =await res.json();
console.log(users) like my console in dev tools doesnt show any response
you see the response in your server side log. So
devtools = client side
server log = server side
Your fetch looks like it was fetched serverside, so the console.log will also appear in your server side log
devtools = client side
server log = server side
Your fetch looks like it was fetched serverside, so the console.log will also appear in your server side log
just opened my cmder , its filled with logs . thank you
u can close it now
@B33fb0n3
can we re open the issue again ?
@i_lost_to_loba_kreygasm can we re open the issue again ?
It's never closed, but I guess your question was answered, right?
well another issue appeared in the same subject
3 | export default function BlogId(){
4 | let router=useRouter();
> 5 | const { id} = router?.query;
| ^
6 | return <h1>This is User {id}</h1>
7 | }@i_lost_to_loba_kreygasm js
3 | export default function BlogId(){
4 | let router=useRouter();
> 5 | const { id} = router?.query;
| ^
6 | return <h1>This is User {id}</h1>
7 | }
don't you do it like I mentioned?
https://nextjs-forum.com/post/1269214797438783508#message-1269216413990654034 ( <-- click this)
https://nextjs-forum.com/post/1269214797438783508#message-1269216413990654034 ( <-- click this)
can you share the code?
lol
so I can also call fetch same data based on params
where exactly is params coming from ? I only saw props
@i_lost_to_loba_kreygasm where exactly is params coming from ? I only saw props
they are coming from nextjs itself. But they will be only there when you have the correct folder structure (with
/[dynamicParam]/@B33fb0n3 they are coming from nextjs itself. But they will be only there when you have the correct folder structure (with /[dynamicParam]/
const userData=async ()=>{
let user = await fetch(`https://jsonplaceholder.typicode.com/users/${params.id}`).then(res => res.json());
return user
}
export default function UserId({params}){
let user = userData();
return <div>
<p>{user.name}</p>
</div>
} it says params undefined . I understand that they are in different scopes but how to fixmaybe I can pass it
so i tried
export default function UserId({params}){
const userData=async (params)=>{
let user = await fetch(`https://jsonplaceholder.typicode.com/users/${params.id}`).then(res => res.json());
return user
}
let user = userData(params);
return <div>
<p>{user.name}</p>
</div>
}@i_lost_to_loba_kreygasm js
const userData=async ()=>{
let user = await fetch(`https://jsonplaceholder.typicode.com/users/${params.id}`).then(res => res.json());
return user
}
export default function UserId({params}){
let user = userData();
return <div>
<p>{user.name}</p>
</div>
}
it says params undefined . I understand that they are in different scopes but how to fix
when params is underfined, you don't have the correct folder structure like I mentioned here: https://nextjs-forum.com/post/1269214797438783508#message-1269223999217471488
So please take a look at dynamic routes here: https://nextjs.org/docs/app/building-your-application/routing/dynamic-routes
So please take a look at dynamic routes here: https://nextjs.org/docs/app/building-your-application/routing/dynamic-routes
@B33fb0n3 when params is underfined, you don't have the correct folder structure like I mentioned here: https://discord.com/channels/752553802359505017/1269214797438783508/1269223999217471488
So please take a look at dynamic routes here: https://nextjs.org/docs/app/building-your-application/routing/dynamic-routes
I did follow your move , i just had async await issue
but you can close now