Creating An E-Commerce Site With MERN Stack - Part II
Creating An E-Commerce Site With MERN Stack - Part II
35
Retrospection
If you are new to these technologies please read the first part if you are
familiar then enjoy the ride. :)
Introduction
mongoose
.connect("mongodb://127.0.0.1/ECommerce")
res.send("Hello World!");
});
app.listen(port, () => {
});
In the above code, we are connecting to a MongoDB database running on
localhost. Once the connection is established, we can use the mongoose
object to interact with the database.
exports.Product = Product;
In the above code, we are defining a model for a product using the
Mongoose package. The model defines the properties that a product can
have, such as title, description, price, discount percentage, rating, stock,
brand, category, thumbnail, and images. The first line imports the
Mongoose package that we need to create a schema. Then, we create a new
schema for our product using the mongoose.Schema() method. This method
takes an object with key-value pairs, where the key is the name of the
property and the value is an object that describes the property.
Each property has a type and required field. The type field specifies the data
type of the property, and the required field specifies whether the property is
mandatory for a product.
Finally, we export the Product model so that we can use it in other parts of
our application.
Based on our product model add the following routes to the index.js:
await Product.create(newProduct);
res.send("Product saved to the database!");
});
//Get the all product list
app.get("/read", async (req, res) => {
const productList = await Product.find();
res.send(JSON.stringify(productList));
});
Above in the code, you can see the four basic routes for manipulating the
products. For testing without the frontend we need an API testing tool. I’m
going to use Postman (https://www.postman.com/) but if you are familiar
with another API testing tool go for it!
Let’s test!
Using the create method we can add a new product to the database and we
get back a success respond!
Add new product
Using the delete method we can delete a product by id from the database.
Delete a product
Using the get method we can retrieve all products from our database.
Get the products
4. View the documents in the collection and verify that the data is correct.
By performing regular checks in Mongo DB Compass, you can ensure that
your data is accurate and up-to-date. This is particularly important when
working with large amounts of data, such as in an e-commerce website. So
don’t forget to make regular checks to ensure the smooth functioning of your
application!
Retrieving just a single product is very similar to the update function, simply
you need to include the id and use the .findById() function. At the you return
the product object.
I hope you take the exercise as a good challenge and you try to write alone.
Give it a try and see how it goes! If things aren’t going well, don’t worry! You
can always find a solution on GitHub.
Conclusion
43 Followers
Hi everyone! I'm an enthusiastic full-stack developer. Please feel free to reach out to me via
email ([email protected]) or Twitter (@tokosbex).
Mar 18 1 Jan 15 40
Lists
Help Status About Careers Press Blog Privacy Terms Text to speech Teams