Now we have just setup our modals for our database. Now we can setup how we are gonna create, read, update, and delete products in our database. If you haven’t completed the last step, it is recommended you finish previous steps before moving on.

Now lets get the “ball rollin” as they say, and start in our products_controller.js file. We are gonna first import our Product model from our product.js file in our models folder, which we will have access, after going out the controllers folder.

//This will be used to perform full crud in the database.

Now lets read all products from the products database.

First define the readProducts method with a request and a response parameters or req and res for short within our module.exports object.

module.exports = {

Next use the Product model your imported, and invoke the find() method with an empty object, indicating to find a product based on no criteria, which therefore finds all the products in a form of a promise, that we will resolve using the exec function build in the model from mongoose. It takes a callback with 2 parameters a error if received and the data itself.

Product.find({}).exec((err, products) => {
//Always do a couple of console.logs just in case of errors.
if(err) console.log('Get Product Mongoose Error------------------', err);
//Always log the data you are returning from the database to check if you are receiving the right data.

Now the final step in completing this method is returning the data back to the frontend, they are two ways the send method, and json method. The send method just sends the data, and json send an object you define within the invokation of the method.

//After the data is received send a status code of 200 and the data via the send method.

Do not test anything yet, we will do it later. Now we will start getting products based on id.

Now we will finish the product_controller js file with a readProduct method which read specific product based on a criteria, unlike the readProducts. In this case we will specify the id of the product.

readProduct(req, res){
}

We will get the id from the endpoint, which we specified via a colon(:) in our endpoint. Which is called id. Which we will retrieve from the req.params

//Destruct the id from req.params
const { id } = req.params;

Then we will use the findById() method from the model to find a single products based on id. Has the same structure as the find({}), but just takes the id.It will also have a .exec which will resolve the promise, which also take a callback with error as 1st argument, and the data returned as 2nd argument.

//Destruct the id from the endpoint url, used to retrieve  a specific products.

Now our products_controller.js file is finished, we just need to test them using postman which we will cover in the next step.

Here is all the work we done so far.

//Make sure to import it outside of module.exports to have it accessible through out file.
const Product = require('../models/product');

Let start setting up our admin_controller file. Since this controller’s functionality is based on the admin which will have the ability to create, update, and delete products, and users. For now we will have just have the readAdminUsers defined, and the rest of crud of product in this file for now.

Let start by import both our User, and Product model from their respective files within the models folder, and have our methods within the module.exports object.

const Product = require('../models/product');

First within our module.exports object we will define the readAdminUsers method, which will get all the users. It will pretty much the same as the readProducts method we define earlier but with our User model.

 getAdminUsers(req, res) {

Now lets define the rest of our crud with our products. Lets first start with our createProduct which create a new product. Lets first destruct the values we need to create a product, which will be a name, description, price for now from the req.body, or the request.body. We will not need an id, since mongodb auto-generates one(id) by default when creating data.

createProduct(req, res) {
//Destruct the values sent in from frontend from req.body

It is very simple to create a new data, and insert into database. We will first set a variable to an new instance of the Product model, with our values.

//Have a new Product model instance assigned to a variable to be save to database.

Now to insert to database, just use the save method on the object assigned, Then send back the created product.

//use the .save() to save model to database.

That’s it for the creating product!

Now updating is gonna similar to creating a product, but the difference will be that we would also need the id, which we will retrieve from the endpoint.

updateProduct(req, res) {

Now we will retrieve the specific product we want using the findById() method, and assign each property to the values in the request body. Exact same way as readProduct method, but just assign the properties of the product to the values of req.body, and saving the new data to database.

Product.findById(id).exec((err, product) => {

Finally we are almost done! One more step, to delete the product.

Similar to the update in that we are retrieve an id from the req.params just like the updateProduct, but everything else is different.

deleteProduct(req, res) {

We will the product using the deleteOne({}) method that takes an object with the property you want the criteria to be. We will be using the id, which by default in mongodb is _id. It has a .exec which resolve the promise just like the find({}).

//Use an object to delete the specified product.

That’s it for the controller files we will need for most of the tutorial.

Here is the products_controller.js file if you need a reference.

const Product = require('../models/product');

Here is the next step and my code from github.

Next we will setup our package.json and run our server.

Happy Coding

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store