Skip to content

Commit 5593e82

Browse files
committed
Initial Commit
1 parent 3c60ce7 commit 5593e82

File tree

3 files changed

+36
-17
lines changed

3 files changed

+36
-17
lines changed

pages/api/movies.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import clientPromise from "../../lib/mongodb";
2+
3+
export default async (req, res) => {
4+
try {
5+
const client = await clientPromise;
6+
const db = client.db("sample_mflix");
7+
8+
const movies = await db
9+
.collection("movies")
10+
.find({})
11+
.sort({ metacritic: -1 })
12+
.limit(10)
13+
.toArray();
14+
15+
res.json(movies);
16+
} catch (e) {
17+
console.error(e);
18+
}
19+
}

pages/movies.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default function Movies({ movies }) {
1111
{movies.map((movie) => (
1212
<li>
1313
<h2>{movie.title}</h2>
14-
<h3>{movie._id}</h3>
14+
<h3>{movie.metacritic}</h3>
1515
<p>{movie.plot}</p>
1616
</li>
1717
))}
@@ -37,6 +37,6 @@ export async function getServerSideProps(context) {
3737
props: { movies: JSON.parse(JSON.stringify(movies)) },
3838
};
3939
} catch (e) {
40-
console.log(e);
40+
console.error(e);
4141
}
4242
}

pages/top.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,22 @@ export default function Top({ movies }) {
2121
}
2222

2323
export async function getStaticProps() {
24-
try {
25-
const client = await clientPromise;
24+
try {
25+
const client = await clientPromise;
2626

27-
const db = client.db("sample_mflix");
27+
const db = client.db("sample_mflix");
2828

29-
const movies = await db
30-
.collection("movies")
31-
.find({})
32-
.sort({ metacritic: -1 })
33-
.limit(1000)
34-
.toArray();
29+
const movies = await db
30+
.collection("movies")
31+
.find({})
32+
.sort({ metacritic: -1 })
33+
.limit(1000)
34+
.toArray();
3535

36-
return {
37-
props: { movies: JSON.parse(JSON.stringify(movies)) },
38-
};
39-
} catch (e) {
40-
console.log(e);
41-
}
36+
return {
37+
props: { movies: JSON.parse(JSON.stringify(movies)) },
38+
};
39+
} catch (e) {
40+
console.error(e);
41+
}
4242
}

0 commit comments

Comments
 (0)