Help with SSR
Answered
TK-ed posted this in #help-forum
Original message was deleted.
Answered by Ray
1. you cannot use
2. you are using
useEffect in server component2. you are using
useEffect without importing it95 Replies
Original message was deleted
1. you cannot use
2. you are using
useEffect in server component2. you are using
useEffect without importing itAnswer
@Ray 1. you cannot use `useEffect` in server component
2. you are using `useEffect` without importing it
i did import and tried still the same
@TK-ed i did import and tried still the same
you can't use
useEffect in server componentjust call
connectDB directlyhome component can be async
hey i think its working
so ik the folder structure plays an important role in next
say i wanna fetch data from db and work on servers should those files be put in some separate folders??
say i wanna fetch data from db and work on servers should those files be put in some separate folders??
@TK-ed so ik the folder structure plays an important role in next
say i wanna fetch data from db and work on servers should those files be put in some separate folders??
it's up to you. for me, yes I would put it in separate folder
@Ray it's up to you. for me, yes I would put it in separate folder
mann this is so confusing
@TK-ed mann this is so confusing
what is confusing?
@Ray what is confusing?
the serverside things
theres getServerSideProps and getStaticProps, they both do the same job right
theres getServerSideProps and getStaticProps, they both do the same job right
there is no
getServerSideProps nor getStaticProps in app routercheck this course
https://nextjs.org/learn
https://nextjs.org/learn
thanks and whats app router ðŸ˜
@TK-ed thanks and whats app router ðŸ˜
its app router when you using
app folderohh so i cant use ssrprops when using that?
getServerSideProps and getStaticProps are for page router which use pages folder@TK-ed ohh so i cant use ssrprops when using that?
in app router, you don't need
getServerSideProps, just fetch inside the componentright does that mean im good using just callback functions??
just execute the function in the component
right
by default, the component is a server component
lets say my main page is a client comp can i declare a function in there as a server??
it is better to leave the page as server component because metadata api need to be used with server component
uhhh
can i dm?
better not, I have lot of unread dm

im really confused and excited at the same time lol
alright 

why doesnt it get passed as props??
what is that
man it isn't how nextjs work
@TK-ed T_T
move the fetching inside the Home component
you don't need
getProps()@Ray you don't need `getProps()`
My dumb knowledge just got more dumb 😵
@Ray in app router, you don't need `getServerSideProps`, just fetch inside the component
can i use this in src structure?
@TK-ed can i use this in src structure?
it only available in page router
npx create-next-app@latest and choose no when it ask do you want to use app routerI’ll let yk if there anything else
please do not delete the question after it has been answered because (1) the answer will look awkward without the context and (2) other people cannot benefit from the answer as well. if your question includes sensitive data, you should edit the question to remove it instead (but you should never reveal any sensitive data in public forums like this one)
whenever i call the signup route it says 404 not found
@TK-ed whenever i call the signup route it says 404 not found
it should be
/pages/api/signup.js if you are using page routeri switched to app router and some server error
Can you show how you switched to the App router?
@TK-ed im always getting some kind of error whenever i do server stuffs
import { NextResponse } from "next/server";Does it need to be an API route? If you're always going to be calling this API from inside, just make it a server action IMO
https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions-and-mutations
https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions-and-mutations
@Ray `import { NextResponse } from "next/server";`
im not getting any suggestions so its kinda hard
@Mozzy Does it need to be an API route? If you're always going to be calling this API from inside, just make it a server action IMO
<https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions-and-mutations>
i tried creating an object and saving in db that doesnt work either
@TK-ed i tried creating an object and saving in db that doesnt work either
"doesn't work" isn't very helpful to us
@Mozzy "doesn't work" isn't very helpful to us
500 error code
wait a min
Also, why is your page.js a client component?
um i was using some hooks earlier so i changed it back then
That doesn't show the error shown in the console. You're also not helping yourself by not using TypeScript
@Mozzy That doesn't show the error shown in the console. You're also not helping yourself by not using TypeScript
im not used to ts and i needed to submit this asap inorder to get into the next round T_T
Are you in a competition or something? Or is this for school or something?
Im in college and this is a task given for me by a company
like inorder for me to get placed or hired i must clear their rounds and tasks
Either way, I would use server actions instead of creating internal API routes.
Server actions are just functions that run on the server that you can call both from server and client components.
@TK-ed Click to see attachment
const reqBody = await NextRequest.json()to
export async function POST(req: NextRequest) {
const reqBody = await req.json()
}@TK-ed like inorder for me to get placed or hired i must clear their rounds and tasks
I don't think you will get hired this time. try again in the next time 😆
@Ray ts
const reqBody = await NextRequest.json()
to
ts
export async function POST(req: NextRequest) {
const reqBody = await req.json()
}
isnt this ts and i still cant understand these server functions TwT
NextRequest is a class
@Ray you should read this
https://nextjs.org/learn
take this course and prepare for next time😂
lol dont give up 

you need time to learn
@Ray NextRequest is a class
express was simple T_T
express is old
use hono ðŸ‘ðŸ¾
@Ray you need time to learn
if only i can somehow make this work
@Ray use hono ðŸ‘ðŸ¾
oooh thats new
I don't think Hono or Express are super relevant here. The app router's API routes really give you what you need here. You just need to be willing to read the documentation or the course.
Don't rush into a job just for the sake of getting a job
The task for won't be easier just because you're paid to do it
@Mozzy Don't rush into a job just for the sake of getting a job
i mean yyeah but still T_T i can atleast experience a new thing right
It won't be a very good experience for anyone if you get fired
Assuming it is Next.js you want to learn, take your time to read through https://nextjs.org/learn. There are also a bunch of great video sources that can teach you both the theory and practical parts. Vercel's Lee comes to mind: https://www.youtube.com/@leerob
aight thanks TxT
@Mozzy It won't be a very good experience for anyone if you get fired
i found the problem mann
not sure why but i used ts instead of js and voila
not sure why but i used ts instead of js and voila
nice
So how can I limit the route.. like I want to allow /login/ok only when username and password are correct
I’m not sure how I can do that but I do have an idea of using the ?: thingy