From the course: Next.js: Creating and Hosting a Full-Stack Site

Unlock this course with a free trial

Join today to access over 24,500 courses taught by industry experts.

Rewriting the load product endpoint

Rewriting the load product endpoint

- [Instructor] Alright, so now that we've rewritten the list products endpoint to use MongoDB, the next endpoint that we're going to do is the endpoint for loading an individual product by its ID. So here, we have the endpoint for that, and what we're going to need to do here is replace our JavaScript logic that filtered through a JavaScript array for the correct product. We're just going to need to change this to a database query. So let's take a look at how to do this. First of all, we're going to need to import that connect to database function that we created, so let's just add that up here. We'll say import, connectToDb, and we'll just let that do the importing for us. And now that we have that, at the top of our route handler, what we're going to do is we're going to say, const db = connectToDb. And again, that is asynchronous, so we're going to need to add the await keyword before that. And now that we have that, the next thing that we're going to do is, as I said, replace this…

Contents