Title: “Adding Changes to Your Web Apps: Mastering CRUD Operations with MERN Stack”

akinbode joshua
2 min readAug 22, 2023

--

Welcome, fellow tech enthusiasts! 🚀 In this guide, I’m diving into the dynamic world of web development using the MERN stack (MongoDB, Express, React, Node.js). Get ready to conquer CRUD operations and build web apps that attracts!

Let’s Get Rolling:

  1. Setting Up the MERN Playground

Before I embark on this coding adventure, ensure you’re all set up. Install MongoDB, set up Node.js, and create your React app. No sweat, I promise!😊😃

2. Creating Data: Embrace the ‘C’ in CRUD

Imagine you’re adding a sprinkle of magic to your app. I’ll show you how to create an API endpoint to make data appear out of thin air.

// Express route to add new data

app.post(‘/items’, (req, res) => {

. // Gather the new item from the request body

. const newItem = req.body;

. // Save newItem to MongoDB using Mongoose

. Item.create(newItem, (err, savedItem) => {

. if (err) {

. res.status(500).send(err);

. } else {

. res.status(201).json(savedItem);

. }

. });

});

3. Reading Data: Unveiling the Mysteries of ‘R’

Ever wished you had a lens for your app’s data? Well I’ve got you covered. You’ll fetch and display data from your MongoDB database, making sure your users see the big picture.

// Express route to get all items

app.get(‘/items’, (req, res) => {

. // Fetch all items from MongoDB using Mongoose

. Item.find({}, (err, items) => {

. if (err) {

. res.status(500).send(err);

. } else {

. res.status(200).json(items);

. }

. });

});

4. Updating Data: The ‘U’ That Unlocks Possibilities

Apps evolve, and so should your data. Get ready to flex your coding muscles as i reveal the secrets of updating records in your app. Your users will love the seamless transformations!

// Express route to update an item

app.put(‘/items/:id’, (req, res) => {

. // Extract the item’s ID from the request parameters

. const itemId = req.params.id;

. // Gather the updated item data from the

request body

. const updatedItem = req.body;

. // Update the item in MongoDB using Mongoose

. Item.findByIdAndUpdate(itemId, updatedItem, { new: true }, (err, item) => {

. if (err) {

. res.status(500).send(err);

. } else {

. res.status(200).json(item);

. }

. });

});

5. Deleting Data: Bid Farewell to Unwanted Entries.

Spring cleaning for your app? I’ll guide you through gracefully removing unwanted data. Watch as records disappear, making your app leaner and meaner.

// Express route to delete an item

app.delete(‘/items/:id’, (req, res) => {

. // Extract the item’s ID from the request parameters

. const itemId = req.params.id;

. // Delete the item from MongoDB using Mongoose

. Item.findByIdAndDelete(itemId, (err) => {

. if (err) {

. res.status(500).send(err);

. } else {

. res.status(204).send();

. }

. });

});

6. Conclusion: Your Journey, Your Apps, Your Success

Congratulations, you’ve leveled up your MERN skills! From creating to deleting, you’ve mastered the art of CRUD operations. Now let’s go forth and build apps that leave a mark on the digital world. 🚀💻

--

--