Getting results as undefined
Answered
Wuchang bream posted this in #help-forum
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
Wuchang breamOP
I am trying to implement server side rendering using nextjs app router but I am getting results as undefined.
I even tried logging the movies variable and i am getting the required result.
I even tried logging the movies variable and i am getting the required result.
import config from "@/config.json";
// Components
import Banner from "@/components/Banner";
const apiKey = process.env.API_KEY;
const Home: React.FC<{}> = async () => {
const movies = await getPopularMovies();
return (
<div className="w-full h-[110vh] grid grid-rows-2 align-center justify-items-center py-10">
<Banner
movieName={movies.results[0].original_title}
movieCover={`${config.tmdbImageURL}${movies.results[0].backdrop_path}`}
/>
</div>
);
};
const getPopularMovies = async () => {
"use server";
var json;
try {
const data = await fetch(
`https://api.themoviedb.org/3/movie/popular?language=en-US&page=1&api_key=${apiKey}`,
{
method: "GET",
cache: "no-store",
}
);
json = await data.json();
} catch (err) {
console.error(err);
}
return json;
};
export default Home;
![Image](https://cdn.discordapp.com/attachments/1195784806022250668/1195784806513000508/Screenshot_2024-01-13_at_23.10.37.png?ex=65b54062&is=65a2cb62&hm=8e76382aaf1ee0252c5c6874a248806cc71847bb7a2cda29c8816265d2d4e1cf&)
Answered by Ray
first, you don't need to use
'use server'
here and try thisimport config from "@/config.json";
// Components
import Banner from "@/components/Banner";
const apiKey = process.env.API_KEY;
const Home: React.FC<{}> = async () => {
const movies = await getPopularMovies();
return (
<div className="w-full h-[110vh] grid grid-rows-2 align-center justify-items-center py-10">
<Banner
movieName={movies?.results[0].original_title}
movieCover={`${config.tmdbImageURL}${movies.results[0].backdrop_path}`}
/>
</div>
);
};
const getPopularMovies = async () => {
const data = await fetch(
`https://api.themoviedb.org/3/movie/popular?language=en-US&page=1&api_key=${apiKey}`,
{
method: "GET",
cache: "no-store",
}
);
return data.json();
};
export default Home;
53 Replies
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
first, you don't need to use
'use server'
here and try thisimport config from "@/config.json";
// Components
import Banner from "@/components/Banner";
const apiKey = process.env.API_KEY;
const Home: React.FC<{}> = async () => {
const movies = await getPopularMovies();
return (
<div className="w-full h-[110vh] grid grid-rows-2 align-center justify-items-center py-10">
<Banner
movieName={movies?.results[0].original_title}
movieCover={`${config.tmdbImageURL}${movies.results[0].backdrop_path}`}
/>
</div>
);
};
const getPopularMovies = async () => {
const data = await fetch(
`https://api.themoviedb.org/3/movie/popular?language=en-US&page=1&api_key=${apiKey}`,
{
method: "GET",
cache: "no-store",
}
);
return data.json();
};
export default Home;
Answer
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
Wuchang breamOP
why no need to use 'use server'?
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
use server
is only for server actionyou are not using server action here
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
Wuchang breamOP
and how should i implement getServerSideProps of page router similar to app router?
can u explain with example?
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
just like this
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
Wuchang breamOP
wdym by a server action
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
the doc have good explanation
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
Wuchang breamOP
but i am sending a request to an api of a backend rather than using with useEffect hook
so ig its a server action only
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
I don't see you are using
useEffect
here![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
Wuchang breamOP
yeah cause i rendered it as a server side rendering
to hide my api keys ykyk
rather than fetching on client side
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
wdym by this?
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
Wuchang breamOP
uh like
i dont understand the meaning of server actions
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
read the doc here
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
Wuchang breamOP
you basically use it to send an api request only right
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
![Image](https://cdn.discordapp.com/attachments/1195784806022250668/1195787003934036099/image.png?ex=65b5426e&is=65a2cd6e&hm=68660edd9ab29c4725c6e7f31c096962479e198ceb9b36d3d4f81f2212fd8d35&)
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
Wuchang breamOP
oh i see
and i did as you said but i am getting an error of "fetch failed"
even removed the "use server"
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
then check the url and the api key
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
Wuchang breamOP
its perfectly fine
i am logging the results and i can see the json data
in terminal
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
so its not fetch failed?
if you can see the result in terminal
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
Wuchang breamOP
![Image](https://cdn.discordapp.com/attachments/1195784806022250668/1195787606944923668/Screenshot_2024-01-13_at_23.22.56.png?ex=65b542fe&is=65a2cdfe&hm=b1313323f46cdedad6ce7ac78ecfe8d86e572910b440d828db1ea3b5d57f0d97&)
this is the component i am passing the props into
"use client";
import Link from "next/link";
import Image from "next/image";
import { Montserrat } from "next/font/google";
const montserrat = Montserrat({
display: "swap",
weight: ["200", "400", "500", "600", "700"],
subsets: ["latin-ext"],
});
type BannerProps = {
movieName: string;
movieDescription?: string;
movieCover: string;
movieLink?: string;
movieGenre?: string[];
};
const Banner: React.FC<BannerProps> = ({
movieName,
movieDescription,
movieCover,
movieLink,
movieGenre,
}) => {
return (
<div className="w-[90vw] h-[75vh] bg-oxford-blue grid grid-cols-3">
<section>
<h1 className="text-white">{movieName}</h1>
</section>
<section className="flex items-center justify-center">
{/* <Image src={movieCover} alt={movieName} fill={true} className="" /> */}
</section>
</div>
);
};
export default Banner;
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
where do you see the json data?
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
Wuchang breamOP
the terminal
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
you mean this?
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
Wuchang breamOP
yus
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
check your url and the api key
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
Wuchang breamOP
should i be using "use server" in the banner component?
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
no
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
Wuchang breamOP
o
see
![Image](https://cdn.discordapp.com/attachments/1195784806022250668/1195788644062085120/Screenshot_2024-01-13_at_23.27.01.png?ex=65b543f5&is=65a2cef5&hm=24f0b0d0aaf5f1a5f7ad6c43836a3b3b476eddcce4eee0c3011db70ba3c98e83&)
o nvm its working now
idk how
so this data is being fetched on the server side and i cant use redux with it right?
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
you could pass the data to it
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
Wuchang breamOP
oh
ok
i will try
thank you
but there is not really a point in using that rn
ig