-
I'd like to fetch the latest blog posts from an external API on an otherwise static page. I use
Using My code: (also on CodeSandbox) import fetch from "node-fetch";
const endpoint = "https://swapi.co/api/films";
export default function TestPage(props) {
return (
<div>
Hello World.
<section>
<h2>There should be a list of titles here</h2>
{typeof props.posts !== "undefined" &&
props.posts.map(film => (
<li key={film.id}>
<strong>{film.title}</strong>
</li>
))}
</section>
<hr />
</div>
);
}
export async function getServerSideProps() {
fetch(endpoint)
.then(postsResponse => {
return postsResponse.json();
})
.then(posts => {
console.log("PAYLOAD TITLE!!!", posts.results[0].title || "nothing?");
return {
// props: { posts: posts.results }
props: { posts: [{ id: 666, title: "Fake Post Title" }] }
};
});
}
This is very similar to #11009 but I'm not sure where else should I export Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
UPDATE: Nevermind, it's my
|
Beta Was this translation helpful? Give feedback.
UPDATE: Nevermind, it's my
.then
Promise that's somehow botched 🤦🏽♀️. Fixed it by usingawait
like the official docs example.