Module 67 Web Development
Module 67 Web Development
There they make a async function there is try and final thing where in the try they
connect the client and then done ping things after that in the finally they close the
client
We can remove that client close if we want our server run all the time, in this case
we will remove this
Now if you run this you will see in cmd that your mondb successfully connected
like this thngs…
Now we will do client site work for this in cmd do this code
Copy the code form rect router
npm create vite@latest name-of-your-project -- --template react
# follow prompts
cd <your new project directory>
npm install react-router-dom localforage match-sorter sort-by
npm run dev
now in the ”app.jsx” function let’s make an if function where if data send to
database then it will give “alert” like this in the handleAdduser function:
if(data,insertedId){
alert(“User added successfully”)here “insertedId” is a id which is automatically
given by mongdb for indiv
idual data which is send to database
from,reset()
}
We done create things from CRUD let’s jump on Read things:
Let’s go to client code there in src folder make a new folder name this Components and make a
file on that Users.jsx
And make route for this jsx on main.jsx and make a loader with a arrow function where we will
fetch like this:
{
Path:’’/users’
Element:<Usert/>,
Loader: () => fetch(‘’)
}
After doing this we have to make a get In the index.js for this users we already made post now
we will make a get upper of post before doing code we have to go to the mongodb doc from ther
in find operations after their find multiple documents
link(https://www.mongodb.com/docs/drivers/node/current/usage-examples/find/) after checking
the doc we will code like this:
app.post("/users", async(req, res) here we make a async function => {
const cursor = userCollection.find(); here we made a variable named this cursor in here
we called userCollection which we made earlier for store our data there after calling
userCollection we use find option bythis in cursor variable our all date will come
const result = await cursor.toArray() here we make a variable named it result here we use
await to come the data after that we call cursor and make this cursor to array by
user ,toArry() because we have to get data in the form of array
res.send(result); here we just simply send result by using res,send
})
{
path:"/users",
element:<Users/>,
loader: ()
=>fetch('http://localhost:5000/users')
}
Now we will go to the Users.jsx and do some code to show the data in UI
We have to make a variable where we call a function useLoaderData which will
take fetch data from main.jsx
Like this:
Const users = useLoaderData();
After that we will map the users data to take single data and show in UI like this:
{
users.map(user => <p
key={user._id}>{user.name} : {user.email}</p>)
}
So we done create (post) and Read(get) from crud now we will make
Delete things let’s do it:
For doing this we will go to the users.jsx in there we will make a button we every
user for delete purpose like this :
<button>x</button>
After that we have to make a handle where the delete function will work after
making that handle we will use this in the delete button like this:
Const handleDelete = _id (here we make a parameter and name this _id because we make
delete button for every single data and whenever the delete button clicked then that specific data
will be deleted we know every data have a specific _id for similarities we also named this
parameter _id) => {
Console.log(_id)
Fetch(‘http://localhost:5000/users/${_id}”)(here we have fetched the backend link
and toke the specific data by using ${_id}) , {
Element:<Update/>
So for updating any user we have to get the user data for that reason we have to use
loader and fetch the link for fetching the link we have to make API in the backend
Loader: ({params}) => fetch(‘loaclhost:5000/users/${params.id}’) (here destructor
the params and get the id params )
Now let’s make API in the backend , so in this case we have to get specific data
not the hole users let’s do this in the inde.js
App,get(“/user/:id”, asyn(req, res)(here we dynamically set our link by :id)=> {
Const id = req.params.id; (here we speiciy the id which will come by params)
Const query = {_id = new ObjectId(id)} (here we set which object and which data we need
here objecr is _id and data Is ObjecId(id))
Res,send(user)
})
Now we will go to the update,jsx
And load the data
Then we will make a form and we will give the defaultvalue which will be loaded
data
Then we will make a handleupdate
Like this:
<form onSubmit={handleUpdate}>
<input type="text" name="name"
defaultValue={loaddedUser?.name} id="" /><br />
<input type="email"
name="email" defaultValue={loaddedUser?.email}
id="" /><br />
<input type="submit"
value="Update" />
</form>
Now let’s go to the backend and make app which will update our user data :
For this we will use put like this:
App.put(‘/update/:id’, async(req, res) => {
Cons id = req.params.id
Const updatedUser = req.body (here we are requesting the specific update page body )
}
Now we will go to the client site there we have to fetch the link
In the fetch we will dynamic the url and the dynamic part will ne loaddedUser
because from there the data is coming and we will specify by the id like this
Fetch(‘url/loaddedUser._id’) and the nex predecessor is same here is the code:
Now we are sending data to the server now we will set this data to the database
Now we will go to the app,put here we will make a variable which name will be
filter in this variable we will specify our id
Const filte = {_id: new obejcrId(id)}
After that we create another variable which will be options which will set update
and insert by using this code:
Const options = {upsert true}
After that we will make another variable which will be updatedUser we also made
same name variable which we changed now and named that user
In this variable we will set the value which will be change the code is:
Const updatedUser = {
$set: {
Name:user.name
email:user,email
}
}
After that we will make a final variable which is result where all this variable will
be forced to work
Code:
Consr result = await userColletction.updateOne(filter, updatedUser, option)
Res,send(result)