How to fetch and display data from MongoDB in React JS | CRUD operations in MongoDB using React JS

Posted by






Fetching Data from Mongo DB and Displaying in React JS

Fetching Data from Mongo DB and Displaying in React JS

When developing web applications, it is common to use a database to store, retrieve, and manipulate data. Mongo DB is a popular choice for many developers due to its flexibility and scalability. In this article, we will look at how to fetch data from a Mongo DB database and display it in a React JS application.

Prerequisites

Before we start, make sure you have the following installed:

  • Mongo DB
  • Node.js
  • React JS

Getting Started

First, we need to create a database and populate it with some data. You can do this using the mongo shell or a GUI tool like MongoDB Compass.

Once the database is set up, we can create a new React JS application using create-react-app:


npx create-react-app mongo-react-app

Next, we need to install a library that will allow us to fetch data from the Mongo DB database. We can use the axios library for this:


npm install axios

Fetching Data

Now that our application is set up, let’s write some code to fetch data from the Mongo DB database. We will create a new component called DataDisplay that will display the fetched data.


import React, { useEffect, useState } from 'react';
import axios from 'axios';

const DataDisplay = () => {
const [data, setData] = useState([]);

useEffect(() => {
const fetchData = async () => {
const result = await axios.get('http://localhost:5000/data');
setData(result.data);
};
fetchData();
}, []);

return (

Data from Mongo DB:

    {data.map((item) => (

  • {item.name}
  • ))}

);
};

export default DataDisplay;

Displaying Data

Now that we have fetched the data, we can display it in our React JS application. We will import the DataDisplay component into our main App component and render it:


import React from 'react';
import DataDisplay from './DataDisplay';

function App() {
return (

Mongo DB Data in React

);
}

export default App;

Conclusion

In this article, we learned how to fetch data from a Mongo DB database and display it in a React JS application. This allows us to create dynamic and interactive web applications that can retrieve and display data from a database. By using axios to fetch data and React JS to display it, we can create a seamless user experience.


0 0 votes
Article Rating
12 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Ashraf shaik
7 months ago

After watching all the videos I got trouble in deployment as we are using nodemon to start server in cmd how can handle when it is on internet

dhiru 7631
7 months ago

sir g par chu kr parnam aap ko
bhout din sai hm yai concept dhundh rahi thi hmko mil nhi raha tha

thanku sir g

Alen Santhosh
7 months ago

Thanks boss..!! really made a useful video

Priyanshu Singh
7 months ago

if anyone getting runtime error just add ? like data?.map

Aditya Ranjan
7 months ago

thanks man 💓

V I S H A L
7 months ago

Thank you so much brother, I was literally trying from 2 days but I was unable to display data on web page My data was totally unable to get in table but wth of you its getting inside table. thanks alot bro..

suman batool
7 months ago

Can you tell how to fetch data based on the search the user inputs from db.

siemano321
7 months ago

thanks man

Bharathi Muthukumar
7 months ago

can you explain how to fetch data by using id and display it in browser

Ranya Boubaker
7 months ago

thanks a lot you saved my life

João Marques
7 months ago

That´s a great video! Please let me know when next video will come out!!

Kiran D
7 months ago

Good content ❤