30
loading...
This website collects cookies to deliver better user experience
npm install
npm start
reactRealm
as the project name, click on Next and then click Create ProjectreactRealmDB
and reactRealmCol
as the database and collection name, and click on Create.getAllUsers
as the function namereactRealmDB
database and reactRealmCol
collection_id
and save it somewhere; we need it for the next function. Then click on Save Draft to create a deployment draft for our function.getSingleUser
as the function namereactRealmDB
database and reactRealmCol
collection_id
. Because MongoDB saves documents in BSON, we need to parse the arg
as BSON using the BSON.ObjectId
.Hello world!
in the exports function with the user’s _id
we copied earlier and then click on Run.editUser
as the function name.id
, name
, location
, and title
argumentsreactRealmDB
database and reactRealmCol
collectionupdated
variable that finds the document by _id
, update the collection fields, and set a returnNewDocument
flag to return the updated document.Hello world!
in the exports function with required arguments(_id, name, location, and title), click on Run, and then Save Draft.createUser
as the function name.name
, location
, and title
arguments.reactRealmDB
database and reactRealmCol
collection.Hello world!
in the exports function with required arguments(name, location, and title), click on Run, and then Save Draft.deleteUser
as the function name.reactRealmDB
database and reactRealmCol
collection.deleteUser
variable for deleting by _id
.Hello world!
in the exports function with the required argument, click on Run, and then Save Draft.npm i realm-web
realm-web
is a library for accessing MongoDB Realm from a web-browser..env
file in the project root directory, and in this file, add the snippet below:REACT_APP_REALM_APP_ID=<your-realm-app-id>
utils
folder in the src
folder, and in this folder, create a mongo.client.ts
file and add the code snippet below:!
in front of REALM_APP_ID
tells the compiler to relax the non-null constraint error(Meaning the parameter cannot be null or undefined)models
folder in the src
folder, and in this folder, create a user.interface.ts
file and add the code snippet below:App.tsx
by updating it with the snippet below:IUser
interface, app
, and credentials
.getUsers
function inside the useEffect
hook to authenticate our application using the credentials
imported, get the list of users by accessing the getAllUsers
serverless function we created earlier, and then update the users
state.
PS: The serverless function (getAllUsers in our case) called must be the same as the one defined on MongoDB Realm.
App.tsx
by creating a state variable to manage returned value when a user is created. We also need to add the state as dependency on the useEffect
hook; so that when changes are made, it refreshes the page and load the latest list of users. Finally, we need to update the Modal
component with the state function to update the value.*Modal.tsx*
component not having the setUserValue property. We will fix this.Modal.tsx
file inside the components
folder, update the interface, and create a user.handleSubmit
function to authenticate our application using the credentials
imported. Create a user by accessing the createUser
serverless function we created earlier, passing the required arguments (name, location, and title)and then updating the userValue
and form state.App.tsx
by creating a state variable to manage the _id
of the user we want to edit. We also updated the onClick
event to update the state variable and pass it as props to the Modal
component.Modal.tsx
and update as shown below:editingId
useEffect
to conditionally check if it’s editing or creating, get the selected user details using the getSingleUser
serverless function and then update the form values. The getSingleUser
function also required us to convert editingId
to string using BSON.ObjectID
function.handleSubmit
function to include updating the user’s details by conditionally checking if it is an update action or not. Next, we need to call the editUser
serverless function and pass in the required parameters. Finally, update the userValue
, restore the form back to default and close the Modal
component.App.tsx
by creating a handleDelete
function as shown below:handleDelete
function that takes an id
as an argument, authenticate our application using the credentials
. Delete selected user using the deleteUser
serverless function and update the userValue
state.