Thanks you for your reply. To clarify a bit, the actual ‘prompt’ from the tutorial is
Blockquote
As a homework exercise, why don’t you create an API route that returns a single movie based on a user provided id? To give you some pointers, you’ll use Next.js Dynamic API Routes to capture the id . So, if a user calls http://localhost:3000/api/movies/573a1394f29313caabcdfa3e , the movie that should be returned is Seven Samurai . Another tip, the _id property for the sample_mflix database in MongoDB is stored as an ObjectID, so you’ll have to convert the string to an ObjectID.
Previously, it has you set up a route in pages/api repository at movies.js, for a long list of movies. Is the ‘.js’ not proper naming convention for routes? I need to set up the movie id as the parameter for the route described in the homework.
Okay, I got to be getting closer…as per the tutorial, first we set up a route to get multiple movies. It is set up at pages/api/movies.js and it looks like this
Then, we are asked to set up the dynamic route that is passing the movie id as a param, I have that set up at pages/api/[movieId].js and it now looks like this
import { ObjectId } from "mongodb";
import clientPromise from "../../lib/mongodb";
export default async (req, res) => {
try {
const id = req.query.id;
const client = await clientPromise;
const db = client.db("sample_mflix");
const movies = db.collection("movies");
const movie = await movies.findOne({ _id:ObjectId(id)});
if(movie){
res.json(movie);
}else{
res.status(404).json({message: "Movies not found"})
}
} catch (e) {
console.error(e);
res.status(500).json({ message: "Internal server error" });
}
};
Update : when I pass a movie id at api/573a1396f29313caabce3f2c, I get “movie not found”, so the else block in [movieId] is running for sure. I have tried multiple movie ids but get the same error for everyone.