<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Hezron Okwach</title>
    <description>The latest articles on DEV Community by Hezron Okwach (@hezronokwach).</description>
    <link>https://dev.to/hezronokwach</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1484092%2Fb755a056-357f-44e2-8374-9604f168922a.jpeg</url>
      <title>DEV Community: Hezron Okwach</title>
      <link>https://dev.to/hezronokwach</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hezronokwach"/>
    <language>en</language>
    <item>
      <title>Gemini Screen Scribe</title>
      <dc:creator>Hezron Okwach</dc:creator>
      <pubDate>Mon, 16 Mar 2026 19:31:25 +0000</pubDate>
      <link>https://dev.to/hezronokwach/gemini-screen-scribe-3402</link>
      <guid>https://dev.to/hezronokwach/gemini-screen-scribe-3402</guid>
      <description></description>
    </item>
    <item>
      <title>The Hidden Cost of Bad Data: Why Clean Data is Worth More Than Gold</title>
      <dc:creator>Hezron Okwach</dc:creator>
      <pubDate>Fri, 26 Sep 2025 10:23:45 +0000</pubDate>
      <link>https://dev.to/hezronokwach/the-hidden-cost-of-bad-data-why-clean-data-is-worth-more-than-gold-afm</link>
      <guid>https://dev.to/hezronokwach/the-hidden-cost-of-bad-data-why-clean-data-is-worth-more-than-gold-afm</guid>
      <description>&lt;h2&gt;
  
  
  When Numbers Lie, Companies Die
&lt;/h2&gt;

&lt;p&gt;Unity Technologies lost $110 million in three months. This was not due to a cyber attack or a failed product launch, but rather to corrupted training data that had poisoned their ad targeting algorithms. Their machine learning models were making decisions based on inaccurate information, and nobody noticed until their quarterly earnings dropped significantly.&lt;/p&gt;

&lt;p&gt;Clean data isn't just another tech buzzword. It's the difference between creating successful products and losing customers to competitors while you're dealing with production disasters.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Real Problem Nobody Talks About
&lt;/h2&gt;

&lt;p&gt;Here's what will actually be happening in 2025: organisations will be drowning in data quality debt, and most won't even realise it. While everyone is obsessed with AI and machine learning, they are feeding these systems toxic information that exacerbates the problem.&lt;/p&gt;

&lt;p&gt;The problem is widespread. Developers inherit broken APIs. Data scientists spend 60% of their time cleaning datasets instead of building models. Customer support teams handle complaints generated by algorithmic errors. Executives make strategic decisions based on dashboards displaying inaccurate figures.&lt;/p&gt;

&lt;p&gt;Most companies think that throwing more AI at the problem will solve it. Wrong. AI exponentially amplifies garbage data. One bad training example can corrupt an entire model, affecting millions of users. It's like giving a racing driver faulty instruments and wondering why they keep crashing.&lt;/p&gt;

&lt;p&gt;The worst part? These failures happen slowly. Customer satisfaction drops gradually. Marketing campaigns become less effective over time. Revenue forecasts miss targets by wider margins each quarter. By the time anyone realises what is happening, millions have already been lost.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Price of Ignoring Reality
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Money Down the Drain
&lt;/h3&gt;

&lt;p&gt;On average, organisations lose $12.9 million annually due to data quality issues. This is not just a waste of accounting or marketing budgets. It's real money disappearing because systems make the wrong decisions based on incorrect information.&lt;/p&gt;

&lt;p&gt;The 1x10x100 rule illustrates how this can become so costly so quickly. Fix a data error at entry and it costs $1; fix it after it's in your system and it costs $10. Fix it after customers see it: $100. Unity learned this the hard way when their corrupted training data had already influenced millions of ad placements.&lt;/p&gt;

&lt;p&gt;Financial services are hit hardest. Banks report average annual losses of $15 million from data quality problems alone, not to mention regulatory fines when compliance systems make decisions based on inaccurate information. When Equifax sent lenders incorrect credit scores for three weeks, the Consumer Financial Protection Bureau fined them $15 million. The hidden cost? Thousands of consumers were offered worse loan terms because algorithms made decisions based on corrupted data.&lt;/p&gt;

&lt;h3&gt;
  
  
  Decision Making in the Dark
&lt;/h3&gt;

&lt;p&gt;Bad information isn't just costly. It undermines the very basis on which modern businesses operate. Fewer than 0.5% of collected data is analysed, but when even a tiny fraction of that is inaccurate, entire organisations make terrible decisions.&lt;/p&gt;

&lt;p&gt;Consider Amazon's 2017 outage. A single typo in a maintenance command caused major web services to crash for four hours, costing affected companies $150 million in lost revenue. One character. Four hours. $150 million gone.&lt;/p&gt;

&lt;p&gt;Machine learning makes this exponentially worse. Traditional software bugs affect specific features. However, ML models trained on corrupted data make systematic errors on a massive scale. The autonomous vehicle industry learnt this the hard way when biased training datasets led to poor performance in rain and snow..&lt;/p&gt;

&lt;h3&gt;
  
  
  Customer Experience Nightmares
&lt;/h3&gt;

&lt;p&gt;With 71% of consumer data containing errors, every customer interaction becomes a potential disaster. Wrong names in emails. Irrelevant product recommendations. Inconsistent pricing across channels. Customers don't blame the data, though they blame your brand.&lt;/p&gt;

&lt;p&gt;Equifax's coding errors affected real people, such as Nydia Jenkins, a Florida resident. Because of algorithmic mistakes, her car loan payments jumped from $350 monthly to $272 bi-weekly, costing her an extra $2,352 annually. She didn't care about data quality frameworks or validation rules. She just knew her bank had made a mistake.&lt;/p&gt;

&lt;p&gt;Omnichannel experiences suffer most. Customers expect seamless interactions across all touchpoints. When mobile apps show one price, websites show another and customer service quotes a third, trust evaporates instantly.&lt;/p&gt;

&lt;h3&gt;
  
  
  Falling Behind Competitors
&lt;/h3&gt;

&lt;p&gt;Companies with clean data experience 62% higher revenue growth than those grappling with quality issues. This isn't just a correlation. Good data enables better decision-making, which generates more good data, creating a virtuous cycle that competitors cannot access.&lt;/p&gt;

&lt;p&gt;Real-time business operations turn this into a competitive weapon. While your pricing algorithm takes hours to respond to market changes due to data issues, competitors with clean pipelines can react in milliseconds and capture a disproportionate share of the market.&lt;/p&gt;

&lt;p&gt;Innovation suffers when engineering teams spend 27% of their time fixing data issues instead of developing new features. Those hours add up. While your developers are debugging data pipelines, competitors are shipping products that delight customers and create new revenue streams.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Fix This Before It Kills You
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Stop Garbage at the Source
&lt;/h3&gt;

&lt;p&gt;Implement validation rules to check data before it enters your systems. Tools such as Great Expectations and Soda Core offer frameworks for defining business rules and formatting requirements.&lt;/p&gt;

&lt;p&gt;Focus on fields that directly impact revenue or customer experience. Validate email formats in real time. Check phone number patterns. Implement range checks for financial transactions. Make validation invisible to users while preventing corruption from day one.&lt;/p&gt;

&lt;p&gt;Go beyond simple format checks. Implement semantic validation that understands the business context. For example, a birth date in the future is technically valid, but logically impossible. Cross-field validation ensures consistency across related data points.&lt;/p&gt;

&lt;h3&gt;
  
  
  Watch Everything in Real Time
&lt;/h3&gt;

&lt;p&gt;Maintaining data quality requires constant surveillance. Implement monitoring systems that continuously track quality metrics and alert teams when issues arise. Tools such as Monte Carlo and Bigeye offer observability platforms that can automatically detect anomalies.&lt;/p&gt;

&lt;p&gt;Set up tiered alerts based on business impact. Critical issues affecting customer systems should alert engineers immediately. Less urgent problems can generate tickets for the next business day. The goal is to catch problems before they reach customers.&lt;/p&gt;

&lt;p&gt;Establish baseline quality metrics for each dataset and track trends over time. Sudden drops in completeness or spikes in null values often indicate problems further up the chain. Historical trending helps to distinguish normal variation from genuine issues.&lt;/p&gt;

&lt;h3&gt;
  
  
  Build Quality into Development
&lt;/h3&gt;

&lt;p&gt;Just as you would integrate code testing into your CI/CD pipeline, you should also integrate data quality checks. Every deployment should include automated tests to verify schemas, validate business rules and check integration points.&lt;/p&gt;

&lt;p&gt;Define quality requirements as part of your 'definition of done'. Every new data field requires validation rules, monitoring alerts and documentation detailing the expected format. This will prevent the accumulation of technical debt.&lt;/p&gt;

&lt;p&gt;Establish contracts between teams that specify quality expectations, update frequencies and escalation procedures. When marketing uses product data, both parties should agree on formats, completeness requirements and acceptable latency.&lt;/p&gt;

&lt;h3&gt;
  
  
  Automate the Boring Stuff
&lt;/h3&gt;

&lt;p&gt;Use tools that continuously profile data to understand its characteristics and identify quality issues. Automate high-volume, low-risk transformations such as address standardisation and phone number formatting.&lt;/p&gt;

&lt;p&gt;For complex business rules, implement automated detection alongside human approval workflows. This approach strikes a balance between efficiency and accuracy, fostering confidence in automated systems.&lt;/p&gt;

&lt;p&gt;Create feedback loops to improve automated processes over time. When humans correct automated suggestions, capture that feedback to improve future recommendations.&lt;/p&gt;

&lt;h3&gt;
  
  
  Make Everyone Care
&lt;/h3&gt;

&lt;p&gt;Data quality is not just a technical problem. Clearly establish ownership across teams, assigning specific individuals to monitor and maintain standards. Create incentives that reward quality improvements.&lt;/p&gt;

&lt;p&gt;Provide training to help all team members understand the impact of their work on data quality. Developers should familiarise themselves with validation best practices. Product managers should consider quality requirements when designing features.&lt;/p&gt;

&lt;p&gt;Measure and communicate quality metrics at an organisational level. Include data quality KPIs in executive dashboards and team performance reviews. When quality becomes a visible business metric, teams will naturally prioritise it.&lt;/p&gt;

&lt;h2&gt;
  
  
  What's Coming Next
&lt;/h2&gt;

&lt;p&gt;AI-driven data cleaning systems are evolving beyond simple rule-based validation to understand semantic context and business logic. It is predicted that, by 2026, 70% of new applications will incorporate intelligent data quality capabilities.&lt;/p&gt;

&lt;p&gt;Edge computing is reshaping quality requirements as processing moves closer to data sources. As 75% of enterprise data is expected to be processed outside of traditional data centres by 2025, organisations will require distributed quality capabilities.&lt;/p&gt;

&lt;p&gt;The evolution of regulations around data quality is accelerating. The UK's Data Act 2025, for example, introduces new requirements for automated decision-making systems. Organisations are facing increasing legal liability for decisions made using inaccurate data, meaning that quality has become a compliance imperative.&lt;/p&gt;

&lt;p&gt;The emergence of data contracts signifies a shift towards treating quality as an inherent product characteristic rather than a technical afterthought. Organisations are implementing quality SLAs between teams, setting measurable commitments to accuracy, completeness and timeliness.&lt;/p&gt;

&lt;h2&gt;
  
  
  Your Turn
&lt;/h2&gt;

&lt;p&gt;Although data quality problems are universal, the solutions are not. Every system has its own unique failure patterns and risk profiles. The key is to start somewhere instead of waiting for the perfect solution.&lt;/p&gt;

&lt;p&gt;Ready to dive deeper into data reliability? Connect with me on &lt;a href="//www.linkedin.com/in/hezron-okwach"&gt;LinkedIn&lt;/a&gt; or through &lt;a href="mailto:hezronokwach@gmail.com"&gt;hezronokwach@gmail.com&lt;/a&gt;. The companies that master data quality in 2025 will dominate their markets in 2030.&lt;/p&gt;

</description>
      <category>datascience</category>
      <category>analytics</category>
      <category>ai</category>
    </item>
    <item>
      <title>Master Pull Request Messages: Save Your Team's Sanity and Time!</title>
      <dc:creator>Hezron Okwach</dc:creator>
      <pubDate>Mon, 07 Jul 2025 10:42:49 +0000</pubDate>
      <link>https://dev.to/hezronokwach/master-pull-request-messages-save-your-teams-sanity-and-time-158f</link>
      <guid>https://dev.to/hezronokwach/master-pull-request-messages-save-your-teams-sanity-and-time-158f</guid>
      <description>&lt;p&gt;&lt;strong&gt;Ever gotten a PR titled "Fixed stuff" and died a little inside?&lt;/strong&gt; You're not alone.&lt;br&gt;
&lt;strong&gt;47 per cent of devs waste 1-3 hours/week&lt;/strong&gt; deciphering vague PR messages. Yours doesn't have to be part of the problem.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Your PR Message is Secretly the Most Important Code You Write
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;A pull request isn't just code; it's a collaboration handshake.&lt;/strong&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;What it is&lt;/strong&gt;: "Hey team, I made changes. Mind reviewing?" &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Why the message matters&lt;/strong&gt;: 

&lt;ul&gt;
&lt;li&gt;Saves reviewers &lt;strong&gt;hours&lt;/strong&gt; of digging ("&lt;em&gt;Where's the bug? Why this fix?&lt;/em&gt;")
&lt;/li&gt;
&lt;li&gt;Shows respect for your team's time ( "&lt;em&gt;Figure it out yourself&lt;/em&gt;" energy)
&lt;/li&gt;
&lt;li&gt;Documents decisions &lt;strong&gt;forever&lt;/strong&gt; (Future-you will thank present-you)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Anatomy of a Killer PR Message
&lt;/h2&gt;

&lt;p&gt;-&lt;br&gt;
Cut the fluff. Include &lt;strong&gt;only what matters&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Title&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What to Write: Specific change + impact&lt;/li&gt;
&lt;li&gt;❌ Bad Example: &lt;code&gt;Bug fix&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;✅ Good Example: &lt;code&gt;Fix login crash on iOS 15&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Description&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What to Write: &lt;strong&gt;WHAT&lt;/strong&gt; you did + &lt;strong&gt;WHY&lt;/strong&gt; (Context!)&lt;/li&gt;
&lt;li&gt;❌ Bad Example: &lt;code&gt;Made it better&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;✅ Good Example: &lt;code&gt;Added input validation to prevent XSS&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Testing&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What to Write: How you verified (OS/data/edges)&lt;/li&gt;
&lt;li&gt;❌ Bad Example: &lt;code&gt;Seems fine&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;✅ Good Example: &lt;code&gt;Tested on Chrome/iOS w/ 10K chars&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Known Issues&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What to Write: Honesty &amp;gt; perfection&lt;/li&gt;
&lt;li&gt;❌ Bad Example: &lt;em&gt;(Silence)&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;✅ Good Example: &lt;code&gt;Dropdown flickers in Firefox - WIP&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Extras&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What to Write: Screenshots/GIFs/links&lt;/li&gt;
&lt;li&gt;❌ Bad Example: &lt;code&gt;N/A&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;✅ Good Example: &lt;code&gt;Screenshot + Fixes issue #42&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  5 Habits That Make PRs &lt;em&gt;Actually&lt;/em&gt; Helpful
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Speak Human, Not Robot
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;💬 BAD: "Refactored component."
💡 GOOD: "Split PaymentForm into smaller hooks - fixes issue #87 (state mess)."
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Bullet Points &amp;gt; Paragraphs
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;💬 BAD: "Changed button color and moved it to fix layout issues."
💡 GOOD:
- Changed CTA from gray → blue (A/B tested +5 percent clicks)
- Moved button above fold
- Fixed responsive padding on mobile
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Link Obsessively
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;💡 GOOD:
Fixes issue #123
[Design spec](figma.link)
[API contract](docs.link)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. Flag Risks Early
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Heads up: This migration deletes legacy_users table.
Impact: Requires DB backup before merge.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  5. Ask for Specific Reviews
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Reviewer focus:
- Does error handling cover edge cases?
- Is DB rollback safe?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3 PR Sins That Annoy Everyone
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Sin 1: Vague Titles
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;💀 "Fixed."
💡 Fix: "Fix cart total calculation error"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Sin 2: Missing the Why
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;💀 "Updated validation"
💡 Fix: "Added email validation to prevent invalid signups (support ticket #4451)"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Sin 3: Ghost Updates
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;💀 PR body says "Added button" but you removed it later
💡 Fix: Always update description after code changes!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Universal PR Template (Copy-Paste Ready!)
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;### Title
Fix [Issue] in [Module]

### Description
**What**: [Concise summary]
**Why**: [Business/user impact]
**How**: [Key technical change]

### Context
Fixes #[Issue] | Related to #[Epic] | [Design Doc]()

### Verification
- Tested on: [OS/Browser]
- Edge cases: [null inputs/offline/etc]
- Metrics: [Performance gains]

### Known Issues
- [ ] [Bug description] (WIP)

### Extras
- Screenshot: [link]
- Checklist: [x] Tests updated | [ ] Docs needed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Real-World Examples
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Good Example: Authentication Update
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;### Add Google OAuth Login

**What**: Added Google authentication
**Why**: Users requested social login (support issue #301)
**How**: Implemented OAuth2 flow with new API module

### Context
Fixes issue #301 | [Auth design doc](link)

### Verification
- Tested on: iOS/Chrome/Safari
- Edge cases: Revoked permissions, 2FA accounts
- Passes security scan 

### Known Issues
- [ ] Button text overflows on small mobile (fixing in issue #310)

### Extras
![Login screenshot](link)
[OAuth config checklist](link)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Good Example: Multi-Feature PR
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;### Dashboard Updates: Performance + New Widgets

**This PR contains 3 main improvements:**

1. **Performance Optimization**
   - Reduced API calls from 8 → 3
   - Added caching for user data
   - Why: Dashboard loaded slowly for enterprise users

2. **Activity Timeline Widget**
   - Shows recent user actions
   - Collapsible/expandable section
   - Why: PM request #321 (user feedback)

3. **Profile Image Fix**
   - Fixed aspect ratio distortion
   - Added loading skeleton
   - Why: Fixes issue #287 (reported bug)

### Context
Related to dashboard-v2 initiative

### Verification
- Tested with 1000+ activity items
- Verified on: Mobile/Desktop/Dark mode
- Perf metrics: 40 percent faster load time

### Known Issues
- [ ] Timeline pagination not implemented (coming in issue #299)

### Extras
![Dashboard comparison](link)
[Performance benchmark report](link)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Bad Example: Mystery Update
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;### Update
Made auth changes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why this fails:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;❌ Vague title gives zero context&lt;/p&gt;

&lt;p&gt;❌ No explanation of WHAT changed&lt;/p&gt;

&lt;p&gt;❌ No mention of WHY it was needed&lt;/p&gt;

&lt;p&gt;❌ Missing links to related issues/tickets&lt;/p&gt;

&lt;p&gt;❌ The reviewer must read every line of code to understand&lt;/p&gt;

&lt;h3&gt;
  
  
  Good Example: Bug Fix
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;### Fix Profile Image Upload Crash

**What**: Fixed crash when uploading &amp;gt;5MB images
**Why**: Users couldn't upload profile photos (critical!)
**How**: Added client-side file size validation

### Context
Hotfix for production crash P0-447

### Verification
- Tested with: 10MB/100MB/0-byte files
- Verified on: Android Chrome, Safari, Firefox

### Extras
[Screen recording of fix](link)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Bad Example: Lazy Documentation Update
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;### Docs update
Fixed some typos
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why this fails:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;❌ Doesn't specify WHICH docs were updated&lt;/p&gt;

&lt;p&gt;❌ No context about why changes were needed&lt;/p&gt;

&lt;p&gt;❌ No verification (did they check all pages?)&lt;/p&gt;

&lt;p&gt;❌ Forces reviewer to hunt for changes&lt;/p&gt;

&lt;p&gt;❌ Wastes time could be 1 typo or 100 changes&lt;/p&gt;

&lt;h3&gt;
  
  
  Bad Example: Incomplete Feature PR
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;### New search feature
Added search to navbar
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why this fails:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;❌ No explanation of HOW it works&lt;/p&gt;

&lt;p&gt;❌ Missing testing details (what did you test?)&lt;/p&gt;

&lt;p&gt;❌ No links to design specs&lt;/p&gt;

&lt;p&gt;❌ Doesn't mention KNOWN ISSUES:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Search doesn't work on mobile&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;No error handling&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;❌ Reviewer discovers problems during testing&lt;/p&gt;

&lt;h3&gt;
  
  
  Bad Example: Sloppy Refactor
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;### Refactor
Cleaned up code
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why this fails:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;❌ Dangerous title – refactors can break everything&lt;/p&gt;

&lt;p&gt;❌ Zero context about scope or motivation&lt;/p&gt;

&lt;p&gt;❌ No testing information&lt;/p&gt;

&lt;p&gt;❌ Doesn't flag potential risks&lt;/p&gt;

&lt;p&gt;❌ Violates "Always explain WHY" rule&lt;/p&gt;

&lt;h2&gt;
  
  
  Pro Tips for dev.to Readers
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Small PRs &amp;gt; Mega-PRs&lt;/strong&gt;: &amp;lt;300 lines = faster reviews&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Preview before submitting&lt;/strong&gt;: Does it render clearly on GitHub?&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Automate templates&lt;/strong&gt;: Add &lt;code&gt;.github/PULL_REQUEST_TEMPLATE.md&lt;/code&gt; to your repo&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;For multi-feature PRs&lt;/strong&gt;: Start with "This PR contains X main changes:"&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Key Takeaway
&lt;/h2&gt;

&lt;p&gt;A great PR message isn't documentation; it's leadership.&lt;br&gt;
It says, "I respect you, I've done my homework, and I make your job easier."&lt;/p&gt;

&lt;h2&gt;
  
  
  Your Turn:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Steal the template above for your next PR&lt;/li&gt;
&lt;li&gt;Share your worst PR horror story below. &lt;/li&gt;
&lt;li&gt;What's your #1 PR tip? Let's discuss!&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;"The difference between good and great engineers isn't just code – it's communication." - [&lt;a class="mentioned-user" href="https://dev.to/hezronokwach"&gt;@hezronokwach&lt;/a&gt;]&lt;/em&gt;&lt;/p&gt;

</description>
      <category>github</category>
      <category>gitlab</category>
      <category>pullrequest</category>
      <category>coding</category>
    </item>
    <item>
      <title>NEXT.JS AND MONGODB: IMPLEMENTING CRUD OPERATIONS AND TESTING WITH POSTMAN</title>
      <dc:creator>Hezron Okwach</dc:creator>
      <pubDate>Thu, 02 Jan 2025 14:28:14 +0000</pubDate>
      <link>https://dev.to/hezronokwach/building-a-nextjs-back-end-with-mongodb-for-crud-operations-a-guide-to-testing-with-postman-4ne7</link>
      <guid>https://dev.to/hezronokwach/building-a-nextjs-back-end-with-mongodb-for-crud-operations-a-guide-to-testing-with-postman-4ne7</guid>
      <description>&lt;p&gt;Creating a backend for your application can be  straightforward and rewarding, especially when using Next.js alongside  MongoDB. Next.js is a powerful framework that simplifies server-side  development, while MongoDB offers a flexible database solution for  managing data. This guide will walk you through setting up a simple  backend that performs CRUD (Create, Read, Update, Delete) operations  using Next.js and MongoDB. We'll also explore how to test our API using  Postman, making it easy to verify that everything is working correctly.  This step-by-step approach is ideal for beginners eager to learn about  backend development.&lt;/p&gt;

&lt;h2&gt;
  
  
  INTRODUCTION TO NEXT.JS AND MONGODB
&lt;/h2&gt;

&lt;p&gt;In this guide, we will build a simple task manager application focusing  exclusively on the backend using Next.js(&lt;strong&gt;version 15.0.4&lt;/strong&gt;)  and MongoDB. This project will  involve creating robust RESTful APIs that facilitate essential CRUD  operations for managing tasks, where each task will include key  attributes such as a title and a description. Users will be able to  create new tasks, view existing ones, update task details, and delete  tasks as needed. We will utilize Postman to test our APIs and ensure  their functionality throughout the development process. By the end of  this tutorial, you will have a solid understanding of integrating  Next.js with MongoDB and creating dynamic API routes for efficient task  management. &lt;/p&gt;

&lt;h3&gt;
  
  
  Data Interaction Flow: Client, Next.js API, and MongoDB
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://mermaid.live/edit#pako:eNpVj81Ow0AMhF_F2nML9yAh5ecAh6IojYRE04ObuEkgscOuVwU1fXeWAJXwyeP5NCOfTS0NmcgcBznVHVqFMrurGMLEu3ToiTX6kbk4HZFvn-kAiZWTI7uH9fp-fijLHAp69-R0hmT3RB968-ogzh-hEK-0_w1MFjwVZqrVgcoM6W4j3EqWQIaKB3RXOF3ggtRbdosbsv8FbYkbF4rdJOxohji4FZuVGcmO2Dfhq_M3XxntaKTKRGFt0L5VpuJL4NCrbD-5NpFaTytjxbediY44uKD81KBS1mNrcbxeJ-QXkT99-QLdKGkZ" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmermaid.ink%2Fimg%2Fpako%3AeNpVj81Ow0AMhF_F2nML9yAh5ecAh6IojYRE04ObuEkgscOuVwU1fXeWAJXwyeP5NCOfTS0NmcgcBznVHVqFMrurGMLEu3ToiTX6kbk4HZFvn-kAiZWTI7uH9fp-fijLHAp69-R0hmT3RB968-ogzh-hEK-0_w1MFjwVZqrVgcoM6W4j3EqWQIaKB3RXOF3ggtRbdosbsv8FbYkbF4rdJOxohji4FZuVGcmO2Dfhq_M3XxntaKTKRGFt0L5VpuJL4NCrbD-5NpFaTytjxbediY44uKD81KBS1mNrcbxeJ-QXkT99-QLdKGkZ%3Ftype%3Dpng" alt="intro flowchart" width="262" height="350"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Breakdown of flowchart
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Client (Postman/Web Browser)&lt;/strong&gt;: This represents the user interface where requests are initiated. Users can send HTTP requests to interact with the backend API.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;HTTP Request&lt;/strong&gt;: The arrow labeled "HTTP Request" indicates that the  client sends a request to the Next.js API route. This could be any type  of request (GET, POST, PUT, DELETE) depending on the operation being  performed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Next.js API Route&lt;/strong&gt;: This component represents the server-side logic that processes incoming requests. The API route handles routing and business logic, determining how to respond based on the request type and  parameters.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Connects to MongoDB Database&lt;/strong&gt;: Once the API route receives a request, it connects  to the MongoDB database to perform operations such as retrieving or  modifying data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Returns Data&lt;/strong&gt;: After executing the necessary database operations  (like fetching data), MongoDB returns the requested data back to the  Next.js API route.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sends Response&lt;/strong&gt;: Finally, the API route sends an HTTP response back  to the client, which may include data or confirmation of an action taken (e.g., task created, updated, or deleted).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Additionally, the entire code for this project will be accessible in my GitHub repository at &lt;a href="https://github.com/hezronokwach/nextjs-backend.git" rel="noopener noreferrer"&gt;https://github.com/hezronokwach/nextjs-backend.git&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Join me as we dive into this exciting backend implementation journey!&lt;/p&gt;

&lt;h2&gt;
  
  
  SET UP YOUR PROJECT
&lt;/h2&gt;

&lt;p&gt;In this section, we will go through the steps to install and configure Next.js, and MongoDB database.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Install dependencies&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Before starting, ensure that you have Node.js and npm (Node Package Manager) installed on your computer.&lt;br&gt;
&lt;strong&gt;Download Node.js&lt;/strong&gt;: Visit the official Node.js &lt;a href="https://nodejs.org/en/download" rel="noopener noreferrer"&gt;website&lt;/a&gt; and download the latest version suitable for your operating system.&lt;br&gt;
&lt;strong&gt;Verify Installation&lt;/strong&gt;: After installation, open your terminal and run the following commands to check if Node.js and npm are installed correctly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;node &lt;span class="nt"&gt;-v&lt;/span&gt;
npm &lt;span class="nt"&gt;-v&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Create a New Next.js Project&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Next, create a new Next.js application using the following command in your terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx create-next-app nextjs-backend
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will generate a new directory called &lt;code&gt;nextjs-backend&lt;/code&gt;, containing all the necessary files and folders for a Next.js project.&lt;br&gt;
You can change the name of the directory to your liking.&lt;/p&gt;

&lt;p&gt;On installation, you'll see the following prompts:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Would you like to use TypeScript? No / Yes
Would you like to use ESLint? No / Yes
Would you like to use Tailwind CSS? No / Yes
Would you like your code inside a &lt;span class="sb"&gt;`&lt;/span&gt;src/&lt;span class="sb"&gt;`&lt;/span&gt; directory? No / Yes
Would you like to use App Router? &lt;span class="o"&gt;(&lt;/span&gt;recommended&lt;span class="o"&gt;)&lt;/span&gt; No / Yes
Would you like to use Turbopack &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="sb"&gt;`&lt;/span&gt;next dev&lt;span class="sb"&gt;`&lt;/span&gt;?  No / Yes
Would you like to customize the import &lt;span class="nb"&gt;alias&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`&lt;/span&gt;@/&lt;span class="k"&gt;*&lt;/span&gt;&lt;span class="sb"&gt;`&lt;/span&gt; by default&lt;span class="o"&gt;)&lt;/span&gt;? No / Yes
What import &lt;span class="nb"&gt;alias &lt;/span&gt;would you like configured? &lt;span class="o"&gt;(&lt;/span&gt;@/&lt;span class="k"&gt;*&lt;/span&gt; by default&lt;span class="o"&gt;)&lt;/span&gt;? … No / Yes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Options When Creating a Next.js App
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Would you like to use TypeScript?&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Yes&lt;/strong&gt;: Choosing  TypeScript allows you to use a statically typed language that can help catch errors during development and improve code quality. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;No&lt;/strong&gt;: If you select this option, your project will be set up using plain JavaScript.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Would you like to use ESLint?&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Yes&lt;/strong&gt;: Enabling ESLint  integrates a tool that helps identify and fix problems in your  JavaScript code, ensuring best practices and consistency. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;No&lt;/strong&gt;: If you skip this option, your project will not include linting capabilities by default.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Would you like to use Tailwind CSS?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Yes&lt;/strong&gt;: Selecting this  option sets up Tailwind CSS, a utility-first CSS framework that allows  for rapid UI development with pre-defined classes. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;No&lt;/strong&gt;: If you choose not to use Tailwind CSS, you'll need to style your application using traditional CSS or another framework.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Would you like your code inside a &lt;code&gt;src/&lt;/code&gt; directory?&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Yes&lt;/strong&gt;: This option organizes your code by placing it inside a &lt;code&gt;src&lt;/code&gt; directory, which can help keep the project structure cleaner and more manageable. &lt;br&gt;
&lt;strong&gt;No&lt;/strong&gt;: Choosing not to use a &lt;code&gt;src&lt;/code&gt; directory means your code will reside in the root of your project.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Would you like to use App Router? (recommended)&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Yes&lt;/strong&gt;: The App Router is the recommended way to handle routing in Next.js applications,  providing features like nested routes and layouts. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;No&lt;/strong&gt;: If you opt out of  using the App Router, you'll be using the Pages Router instead, which is the traditional routing method in Next.js.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Would you like to use Turbopack for &lt;code&gt;next dev&lt;/code&gt;?&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Yes&lt;/strong&gt;: Turbopack is an experimental bundler designed for fast builds and refreshes during development. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;No&lt;/strong&gt;: If you choose not to use Turbopack, your application will use Webpack as the default bundler.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Would you like to customize the import alias (&lt;code&gt;@/\*&lt;/code&gt; by default)?&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Yes&lt;/strong&gt;: This option allows you to set up custom import paths, making it easier to manage imports throughout your project. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;No&lt;/strong&gt;: If you skip this option, the default import alias will be used.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What import alias would you like configured? (@/* by default)?&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;Here, you can specify a custom alias for imports if you chose &lt;strong&gt;Yes&lt;/strong&gt; in the previous question. For example, setting it to &lt;code&gt;@/&lt;/code&gt; allows you to import modules more easily.&lt;/p&gt;

&lt;p&gt;You can choose the following options for this guide:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk2378rf1m3d3x4jv540q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk2378rf1m3d3x4jv540q.png" alt="Setup options" width="756" height="126"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Navigate to Your Project Directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;nextjs-backend
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For this tutorial, we will be using Visual Studio Code (VS Code) as our integrated development environment (IDE). If you haven't installed it yet, you can easily download it from the &lt;a href="https://code.visualstudio.com/download" rel="noopener noreferrer"&gt;official website&lt;/a&gt; . Once you have Visual Studio Code installed, follow these steps to open your Next.js project:&lt;/p&gt;

&lt;p&gt;Open Your Project in Visual Studio Code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;code &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To run your Next.js application, execute the following command in your terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm run dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By default, the Next.js server will run on localhost at port 3000. You can view your application by opening your web browser and entering  the following address:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;http://localhost:3000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, if you need to use a custom port for any reason—such as  conflicts with other applications or specific deployment  requirements—Next.js provides several options to set a different port:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Modify the &lt;code&gt;package.json&lt;/code&gt; File&lt;/strong&gt;: You can change the default port by updating the &lt;code&gt;dev&lt;/code&gt; script in your &lt;code&gt;package.json&lt;/code&gt; file. For example, to run your application on port 4000, you would modify it like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"dev"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"next dev -p 4000"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"build"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"next build"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"start"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"next start"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Use Command-Line Options&lt;/strong&gt;: You can specify a custom port directly when starting your application by using the command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm run dev &lt;span class="nt"&gt;--&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; 4000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For this guide, we will use the default port 3000 to keep things simple and straightforward. This default page will load after the page has loaded&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F19nuz66e96fojv80sbfa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F19nuz66e96fojv80sbfa.png" alt="Default homepage" width="800" height="405"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Folder Structure
&lt;/h3&gt;

&lt;p&gt;Navigate to the &lt;code&gt;app&lt;/code&gt; folder. Below is how the folder structure looks like:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F68jwec0ge1rgmg358tdp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F68jwec0ge1rgmg358tdp.png" alt="Folder structure" width="325" height="319"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;app&lt;/code&gt; directory is integral to Next.js as it allows developers to define routes based on the file structure. Each folder can contain a &lt;code&gt;page.js&lt;/code&gt; file, which serves as a page component for that specific route. This means that the structure of your folders directly corresponds to the routes in your application i.e the name of the folders inside the &lt;code&gt;app&lt;/code&gt; will be the name of your routes. For instance, if you create a folder named &lt;code&gt;about&lt;/code&gt; with a &lt;code&gt;page.js&lt;/code&gt; inside it, this will be accessible at the /about URL.&lt;br&gt;
Now let us understand the contents of the app folder:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;page.js&lt;/code&gt;:
This file defines the main component for the root route of your application. It is automatically mapped to the root URL (e.g., &lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;http://localhost:3000&lt;/a&gt;). When users visit this URL, the content of page.js is rendered.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;layout.js&lt;/code&gt;:
This file is used to define a layout for your application. It allows you to create a consistent structure across multiple pages, such as headers, footers, and sidebars. Any components or elements defined here will be included in every page that uses this layout.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;font/&lt;/code&gt; Folder:
The font folder is typically used to store custom fonts that you want to use in your application. You can include font files (like .woff, .woff2, or .ttf) here and reference them in your CSS files to ensure consistent typography across your site.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;global.css&lt;/code&gt;:
The global.css file is where you can define global styles for your application. This file is imported into your application to apply styles universally across all components and pages.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;favicon.ico&lt;/code&gt;:
The favicon.ico file is an icon that represents your website in browser tabs, bookmarks, and other places where the site is referenced. It’s a small image (typically 16x16 or 32x32 pixels) that helps users identify your site visually.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Update the Home Page Component
&lt;/h3&gt;

&lt;p&gt;Now let us open the &lt;code&gt;page.js&lt;/code&gt; and replace the contents with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Home&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Hello&lt;/span&gt; &lt;span class="nx"&gt;Next&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;Js&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Code Explanation
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Exporting the Component&lt;/strong&gt;: &lt;code&gt;export default&lt;/code&gt;: This syntax is used to export the &lt;code&gt;Home&lt;/code&gt; function as the default export of this module. This means that when  this module is imported elsewhere, it will import this specific  component by default. In Next.js, each page is typically defined as a  React component, and exporting it allows Next.js to render this  component when users navigate to the corresponding route.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Defining a Functional Component&lt;/strong&gt;: &lt;code&gt;function Home()&lt;/code&gt;: This defines a functional component named &lt;code&gt;Home&lt;/code&gt;. In React (and by extension, Next.js), components are the building  blocks of the user interface. A functional component is a JavaScript  function that returns JSX (JavaScript XML), which describes what the UI  should look like.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Returning JSX&lt;/strong&gt;: &lt;code&gt;return ( &amp;lt;h1&amp;gt;Hello Next-Js&amp;lt;/h1&amp;gt; );&lt;/code&gt;: The &lt;code&gt;return&lt;/code&gt; statement is where you define what the component will render. In this case, it returns an &lt;code&gt;&amp;lt;h1&amp;gt;&lt;/code&gt; element containing the text "Hello Next-Js". &lt;strong&gt;JSX&lt;/strong&gt;: JSX is a syntax  extension for JavaScript that looks similar to HTML. It allows you to  write HTML-like code within JavaScript, which React then transforms into actual DOM elements.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Rendering in Next.js&lt;/strong&gt;: When this &lt;code&gt;Home&lt;/code&gt; component is placed in the &lt;code&gt;page.js&lt;/code&gt; file within the &lt;code&gt;app&lt;/code&gt; directory of a Next.js project, it becomes the content displayed at the root URL (e.g., &lt;code&gt;http://localhost:3000&lt;/code&gt;). When users visit this URL, they will see "Hello Next-Js" displayed on their screen.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5u8l991vmhakwqgpx11c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5u8l991vmhakwqgpx11c.png" alt="create options" width="800" height="409"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;While this section introduces how to set up a basic page in Next.js, our primary focus in this tutorial will be on backend development. We will concentrate on setting up API routes, connecting to &lt;code&gt;MongoDB&lt;/code&gt;, and implementing CRUD operations. This version maintains a technical tone while clearly explaining what is necessary without excessive elaboration. Let move to the next section which is setting up &lt;code&gt;MonngoDB&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  SET UP MONGODB
&lt;/h2&gt;

&lt;p&gt;Before diving into the setup process, it's important to understand some key terms related to &lt;code&gt;MongoDB&lt;/code&gt;. Familiarising yourself with these  concepts will make it easier to navigate the platform and comprehend how it works.&lt;/p&gt;

&lt;h3&gt;
  
  
  Key Terms in MongoDB
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Database&lt;/strong&gt;: A database in &lt;code&gt;MongoDB&lt;/code&gt;is a container for collections. It organises your data into  manageable units, allowing you to store related information together.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Collection&lt;/strong&gt;: A  collection is akin to a table in relational databases. It is a grouping  of documents that share similar characteristics. Each document within a  collection can have different structures, but they typically represent  similar types of data, such as user profiles or product listings.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Document&lt;/strong&gt;: A document is the basic unit of data in &lt;code&gt;MongoDB&lt;/code&gt;, represented in a JSON-like format  (BSON). It consists of key-value pairs and can include various data  types, such as strings, numbers, arrays, and even nested documents.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cluster&lt;/strong&gt;: A cluster is a group of servers (nodes) that work together to store and manage your  data in &lt;code&gt;MongoDB&lt;/code&gt;. Clusters provide redundancy and scalability, ensuring  that your application can handle increased loads and remain available  even if one server fails.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Node&lt;/strong&gt;: A node is an  individual server within a cluster. Each node can store data and process requests, contributing to the overall performance and reliability of  the cluster.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;BSON (Binary JSON)&lt;/strong&gt;: BSON  stands for Binary JSON and is the binary representation of JSON-like  documents used by MongoDB to store data. It allows for more efficient  storage and retrieval compared to standard JSON by supporting additional data types (like dates) and being more compact in certain cases. For  example, a simple -&lt;strong&gt;JSON&lt;/strong&gt;(JavaScript Object Notation) object might look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="s2"&gt;"name"&lt;/span&gt;: &lt;span class="s2"&gt;"Alice"&lt;/span&gt;,
  &lt;span class="s2"&gt;"age"&lt;/span&gt;: 30  
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;URI (Uniform Resource Identifier)&lt;/strong&gt;: The URI is a connection string used to connect your application to the  MongoDB database. It contains essential information such as the  username, password, cluster address, and database name. This string is  crucial for establishing a connection between your application and the  database.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Index&lt;/strong&gt;: An index is a  data structure that improves the speed of data retrieval operations on a collection. By creating indexes on specific fields, you can  significantly enhance query performance, making it easier to find  documents quickly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Aggregation&lt;/strong&gt;:  Aggregation refers to the process of processing multiple documents and  returning computed results. It allows you to perform operations like  filtering, grouping, and sorting on your data to extract meaningful  insights.&lt;/p&gt;

&lt;p&gt;After familiarizing yourself with these key terms related to  &lt;code&gt;MongoDB&lt;/code&gt;, we can now move on to the setup process. Understanding these  concepts will provide a solid foundation as we configure MongoDB for our application. Let’s get started with setting up your MongoDB cluster!&lt;/p&gt;

&lt;h3&gt;
  
  
  Set up
&lt;/h3&gt;

&lt;p&gt;We will be using MongoDB Atlas for this project, leveraging its  cloud access to take advantage of features like automatic scaling, easy  management, and enhanced security. This approach allows us to focus on  development without worrying about the complexities of server  maintenance. Let's proceed with setting up our MongoDB cluster in the  cloud!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; Go to the  official &lt;code&gt;MongoDB&lt;/code&gt;&lt;a href="https://www.mongodb.com/" rel="noopener noreferrer"&gt; website&lt;/a&gt; and create an atlas account. You can use either Google account  or GitHub account.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; After signing in, you'll be  redirected to the main page. If this is your first time creating a  database in &lt;code&gt;MongoDB&lt;/code&gt;, click the drop-down button on &lt;code&gt;project0&lt;/code&gt; in the top left corner. If you’ve created projects before, the name will reflect your latest project. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiumuwdpkoj2dq4bryo8x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiumuwdpkoj2dq4bryo8x.png" alt="Project page" width="800" height="415"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; Next, input the name of your project. For this tutorial, we’ll use &lt;code&gt;nextjs-backend&lt;/code&gt;. After entering the name, click the &lt;strong&gt;Next&lt;/strong&gt; button. &lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8or6i6i59tb02tk1gsu1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8or6i6i59tb02tk1gsu1.png" alt="project name" width="800" height="415"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; On the following page, you  can add members who will have access to the database. In this case, no  additional members will be added, so simply click the &lt;strong&gt;Create Project&lt;/strong&gt; button. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8p3nlwm5g5k24vketdh6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8p3nlwm5g5k24vketdh6.png" alt="project name" width="800" height="415"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5:&lt;/strong&gt; Once the project is created, you’ll be taken to the cluster page where a cluster needs to be set up. Click on the &lt;strong&gt;Create&lt;/strong&gt; button. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh9ztgls59tomdm31owge.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh9ztgls59tomdm31owge.png" alt="project cluster" width="800" height="415"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 6:&lt;/strong&gt; After clicking &lt;strong&gt;Create&lt;/strong&gt;,  you’ll be redirected to the deployment options for your cluster. Choose  the free option; feel free to change the name of the cluster, provider,  and region as desired. For this guide, selecting the free option with  default settings is sufficient. Click on the &lt;strong&gt;Create Deployment&lt;/strong&gt; button at the bottom right.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxz6aa2abszhce5129aq2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxz6aa2abszhce5129aq2.png" alt="cluster" width="800" height="415"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 7:&lt;/strong&gt;  You’ll arrive at  the connection page where you’ll see your username and password. Copy  down the password and then click on &lt;strong&gt;Create Database User&lt;/strong&gt;, followed by &lt;strong&gt;Choose Connection Method&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp5bivwx9qgcb3eep57na.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp5bivwx9qgcb3eep57na.png" alt="connection" width="800" height="415"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 8:&lt;/strong&gt; Let us go back to the virtual studio and lets create a &lt;code&gt;.env&lt;/code&gt; file in the root folder and paste the password.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6mo3n6no1lewbnux5irw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6mo3n6no1lewbnux5irw.png" alt="env file" width="800" height="404"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 9:&lt;/strong&gt; You will be redirected to the page to choose the connection method. Choose the drivers options&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnmvze9rv4p2kckyemk5g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnmvze9rv4p2kckyemk5g.png" alt="project driver " width="800" height="415"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 10:&lt;/strong&gt; You will be redirected to the page where you will choose the driver. Choose the &lt;code&gt;Node.js&lt;/code&gt; option and let the version be by default.  You can then copy the &lt;code&gt;MongoDB URI&lt;/code&gt; in option 3. Click &lt;strong&gt;Done&lt;/strong&gt; button afterwards.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm2seu7uk68xz521a51cz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm2seu7uk68xz521a51cz.png" alt="project driver" width="800" height="415"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 11:&lt;/strong&gt; Lets go to the Virtual Studio and paste the URI in the &lt;code&gt;.env&lt;/code&gt; file. Delete the last part of the URI &lt;code&gt;&amp;amp;appName=Cluster0&lt;/code&gt; since we will give the name of the table in the code while setting up the connection with the database.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9np5trj73ujfvrh6gj68.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9np5trj73ujfvrh6gj68.png" alt="URI" width="800" height="404"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 12:&lt;/strong&gt; After clicking &lt;strong&gt;Done&lt;/strong&gt; , you will redirected to the clusters page where you will see the cluster name(&lt;strong&gt;Cluster0&lt;/strong&gt;). &lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1mm2izhyk3z7jewhcjkb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1mm2izhyk3z7jewhcjkb.png" alt="project cluster overview" width="800" height="415"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can also obtain the &lt;code&gt;URI&lt;/code&gt; by clicking on the &lt;strong&gt;Connect&lt;/strong&gt; or &lt;strong&gt;Get Connection&lt;/strong&gt; String button. This step is crucial if you decide to clone your project onto  another machine, as the URI will not be visible. This is because we will place the &lt;code&gt;.env&lt;/code&gt; file in the &lt;code&gt;.gitignore&lt;/code&gt; file to keep sensitive information secure. If you've already copied the URI and placed it in the &lt;code&gt;.env&lt;/code&gt; file, you can skip this step &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1mm2izhyk3z7jewhcjkb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1mm2izhyk3z7jewhcjkb.png" alt="project cluster overview" width="800" height="415"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F756dou551kjdq2axtkl6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F756dou551kjdq2axtkl6.png" alt="project get string" width="800" height="415"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is the point where the password we copied becomes important because for this URI string will not have the password. Below is how my URI looks like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;mongodb+srv://hezronmackenzie04:&amp;lt;db_password&amp;gt;@cluster0.tmgen.mongodb.net/?retryWrites&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;true&lt;/span&gt;&amp;amp;w&lt;span class="o"&gt;=&lt;/span&gt;majority&amp;amp;appName&lt;span class="o"&gt;=&lt;/span&gt;Cluster0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will replace &lt;code&gt;&amp;lt;db_password&lt;/code&gt;with the password.&lt;/p&gt;

&lt;p&gt;Now let us see how our database looks like. There are two ways do this:&lt;/p&gt;

&lt;h4&gt;
  
  
  Method 1:
&lt;/h4&gt;

&lt;p&gt;The first is by clicking the &lt;code&gt;Browse Collections&lt;/code&gt; on the &lt;code&gt;Overview&lt;/code&gt; page under the &lt;code&gt;Clusters&lt;/code&gt; section:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft60aytskmesv3woehxl4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft60aytskmesv3woehxl4.png" alt="project browse" width="800" height="415"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You will be redirected to the collections page which will have the collection given by default by &lt;code&gt;MongoDB&lt;/code&gt;. It is called &lt;code&gt;sample_mflix&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkrm3wz8nknsdv94qigra.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkrm3wz8nknsdv94qigra.png" alt="project sample db" width="800" height="415"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h5&gt;
  
  
  Method 2:
&lt;/h5&gt;

&lt;p&gt;The other way is by clicking on the clusters on the left side bar under the &lt;code&gt;Database&lt;/code&gt;option:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft60aytskmesv3woehxl4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft60aytskmesv3woehxl4.png" alt="project browse" width="800" height="415"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You will redirected to this page. Choose the &lt;code&gt;Browse Collections&lt;/code&gt; option&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp28qviry7ze4cycru5xw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp28qviry7ze4cycru5xw.png" alt="Browse collection" width="800" height="415"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You will be redirected to the collections page which will have the collection given by default by &lt;code&gt;MongoDB&lt;/code&gt;. It is called &lt;code&gt;sample_mflix&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkrm3wz8nknsdv94qigra.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkrm3wz8nknsdv94qigra.png" alt="project sample db" width="800" height="415"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There is one more step then we are done with the setup.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 13:&lt;/strong&gt; Lets change the network access settings so that our database will be accessible from any place. By default, it will only be accessible through your IP address only. Let us go to the &lt;code&gt;Network Access&lt;/code&gt; option on the left side bar&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkrm3wz8nknsdv94qigra.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkrm3wz8nknsdv94qigra.png" alt="project sample db" width="800" height="415"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 14&lt;/strong&gt; You will redirected to the page listing all the ip addresses which can access the database. Click the &lt;code&gt;ADD IP ADDRESS&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7wt5vbapdaglwh0w5y76.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7wt5vbapdaglwh0w5y76.png" alt="project ip" width="800" height="415"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 15:&lt;/strong&gt; Click on the &lt;code&gt;ALLOW ACCESS FROM EVERYWHERE&lt;/code&gt;  button and click &lt;code&gt;Confirm&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Filczjm1m1w7uxnyffc0m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Filczjm1m1w7uxnyffc0m.png" alt="project  access everywhere" width="800" height="415"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Congratulations on successfully setting up your MongoDB cluster on Atlas! While the setup process may have felt a bit tedious, it’s an  essential foundation for building powerful applications. With this  crucial step behind you, it’s time to shift gears and dive into the  exciting world of coding!&lt;/p&gt;

&lt;h2&gt;
  
  
  CONNECT NEXT.JS TO MONGODB
&lt;/h2&gt;

&lt;p&gt;Now that your database is ready, you’re poised to start  implementing routes and populating your database. This is where the real magic happens! You'll be able to create dynamic APIs that interact with your MongoDB database, allowing you to perform operations like  creating, reading, updating, and deleting data (commonly known as CRUD)&lt;/p&gt;

&lt;p&gt;We will start with writing code that connects the database with the backend&lt;/p&gt;

&lt;h3&gt;
  
  
  Set up Database Connection
&lt;/h3&gt;

&lt;p&gt;Before we proceed with installing &lt;code&gt;mongoose&lt;/code&gt;, let's briefly discuss what it is and what it does. Mongoose is an Object Data Modelling (ODM) library for MongoDB and  Node.js. It provides a straightforward way to model your application  data, allowing you to define schemas for your data structures. With  Mongoose, you can perform operations such as creating, reading,  updating, and deleting documents in your MongoDB database using a more  structured and organized approach. It also includes built-in validation, middleware support, and various other features that make working with  MongoDB easier and more efficient. Now that we understand the role of Mongoose in our application,  let’s install it. &lt;/p&gt;

&lt;p&gt;Open the terminal and run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;mongoose
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let us create a &lt;code&gt;lib&lt;/code&gt; folder in the &lt;code&gt;app&lt;/code&gt; directory. Create a file &lt;code&gt;mongodbConnection.js&lt;/code&gt; file in the &lt;code&gt;lib&lt;/code&gt; folder.&lt;/p&gt;

&lt;p&gt;Add this code to the file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;mongoose&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;mongoose&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;MONGODB_URI&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;MONGODB_URI&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;connectMongoDB&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;mongoose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;MONGODB_URI&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;dbName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;nextjs-backend&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Connected to MongoDB.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;MongoDB connection error:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;connectMongoDB&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Code explanation&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;mongoose&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;mongoose&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Importing Mongoose&lt;/strong&gt;: The code begins by importing the &lt;code&gt;mongoose&lt;/code&gt; library, which provides a straightforward way to interact with MongoDB through schemas and models.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;MONGODB_URI&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;MONGODB_URI&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;MongoDB URI&lt;/strong&gt;: The &lt;code&gt;MONGODB_URI&lt;/code&gt; constant retrieves the connection string from an environment variable.  This string contains the necessary information to connect to your  MongoDB instance, including the username, password, cluster address, and options. Storing sensitive information like credentials in environment  variables helps keep them secure and prevents hard-coding them into your  source code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;connectMongoDB&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Asynchronous Function Declaration&lt;/strong&gt;: The &lt;code&gt;connectMongoDB&lt;/code&gt; function is declared as an asynchronous function using the &lt;code&gt;async&lt;/code&gt; keyword. This allows the use of &lt;code&gt;await&lt;/code&gt; within the function, enabling asynchronous operations to be handled more intuitively.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt; &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;mongoose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;MONGODB_URI&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;dbName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;nextjs-backend&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Connecting to MongoDB&lt;/strong&gt;: The &lt;code&gt;try&lt;/code&gt; block attempts to establish a connection to the MongoDB database using &lt;code&gt;mongoose.connect()&lt;/code&gt;. The first argument is the &lt;code&gt;MONGODB_URI&lt;/code&gt;, which specifies the database location. The second argument is an options object that includes: &lt;code&gt;dbName: "nextjs-backend"&lt;/code&gt;: This specifies the name of the database you want to connect to. In this case, it is set to &lt;code&gt;nextjs-backend&lt;/code&gt;. This allows you to work with this particular database within your MongoDB instance.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Connected to MongoDB.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Success Message&lt;/strong&gt;: If  the connection is successful, a message "Connected to MongoDB." is  logged to the console. This provides immediate feedback that the  application has successfully connected to the database.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;MongoDB connection error:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Error Handling&lt;/strong&gt;: If an error occurs during the connection attempt, it is caught by the &lt;code&gt;catch&lt;/code&gt; block. The error message is logged to the console with "MongoDB  connection error:", followed by the actual error details.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;connectMongoDB&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Exporting the Function&lt;/strong&gt;: Finally, the &lt;code&gt;connectMongoDB&lt;/code&gt; function is exported as the default export of this module. This allows  other parts of your application to import and use this function when  establishing a connection to MongoDB.&lt;/p&gt;

&lt;h3&gt;
  
  
  Set up Models
&lt;/h3&gt;

&lt;p&gt;In this section, we will set up the models for our application.  First, let's clarify what models are and how they will be used in our  project.&lt;/p&gt;

&lt;h4&gt;
  
  
  What Are Models?
&lt;/h4&gt;

&lt;p&gt;In the context of a database, &lt;strong&gt;models&lt;/strong&gt; are representations of the data structures that define how data is  organized and interacted with in your application. They serve as  blueprints for creating, reading, updating, and deleting (CRUD) data in  the database. Each model corresponds to a collection in MongoDB and  defines the schema for the documents within that collection. They define the fields and data types for  each document, as well as any validation rules or methods associated  with that data.&lt;/p&gt;

&lt;h4&gt;
  
  
  Models We Will Use
&lt;/h4&gt;

&lt;p&gt;For this guide, we will create a model for a simple task manager application. The primary model we will be using is:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Task Model&lt;/strong&gt;: This model will represent individual tasks in our application. Each task will have key attributes such as: &lt;/p&gt;

&lt;p&gt;​ &lt;strong&gt;Title&lt;/strong&gt;: A string representing the name of the task. &lt;/p&gt;

&lt;p&gt;​ &lt;strong&gt;Description&lt;/strong&gt;: A string providing details about the task. &lt;/p&gt;

&lt;p&gt;​ &lt;strong&gt;Created At&lt;/strong&gt;: A date indicating when the task was created. &lt;/p&gt;

&lt;p&gt;​ &lt;strong&gt;Updated At&lt;/strong&gt;: A date indicating when the task was last updated.&lt;/p&gt;

&lt;p&gt;By defining this model, we can easily manage tasks within our application and perform CRUD operations on them.Now, let's proceed with creating the &lt;code&gt;models&lt;/code&gt; folder inside the &lt;code&gt;lib&lt;/code&gt; directory and add a file named &lt;code&gt;models.js&lt;/code&gt; to define our Task model.&lt;/p&gt;

&lt;p&gt;Add the code below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;mongoose&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Schema&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;mongoose&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;taskSchema&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Schema&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;timestamps&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Task&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;mongoose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Task&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;mongoose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;model&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Task&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;taskSchema&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;Task&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Code explanation
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;mongoose&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Schema&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;mongoose&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Importing Mongoose&lt;/strong&gt;: The code begins by importing &lt;code&gt;mongoose&lt;/code&gt; and the &lt;code&gt;Schema&lt;/code&gt; class from the Mongoose library. &lt;code&gt;mongoose&lt;/code&gt; provides the functionality to interact with MongoDB, while &lt;code&gt;Schema&lt;/code&gt; is used to define the structure of documents within a collection.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;taskSchema&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Schema&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;timestamps&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Defining the Schema&lt;/strong&gt;: A new schema, &lt;code&gt;taskSchema&lt;/code&gt;, is created using the &lt;code&gt;Schema&lt;/code&gt; constructor. The first argument is an object that defines the fields of the schema: &lt;code&gt;title&lt;/code&gt;: A field of type &lt;code&gt;String&lt;/code&gt; that will store the title of the task. &lt;code&gt;description&lt;/code&gt;: A field of type &lt;code&gt;String&lt;/code&gt; that will store a more detailed description of the task.  The second argument is an options object: &lt;code&gt;timestamps: true&lt;/code&gt;: This option automatically adds two properties to the schema: &lt;code&gt;createdAt&lt;/code&gt; and &lt;code&gt;updatedAt&lt;/code&gt;. These properties track when each document is created and last updated, respectively.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Task&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;mongoose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Task&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;mongoose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;model&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Task&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;taskSchema&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Creating the Model&lt;/strong&gt;: This line checks if a model named "Task" already exists in &lt;code&gt;mongoose.models&lt;/code&gt;. If it does, it uses that existing model to avoid creating multiple  models with the same name. This is particularly useful in environments  like Next.js where modules may be reloaded. If the model does not exist, it creates a new model called "Task" using &lt;code&gt;mongoose.model()&lt;/code&gt;, passing in the name of the model and the schema (&lt;code&gt;taskSchema&lt;/code&gt;). This model represents the "tasks" collection in your MongoDB database.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;Task&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Exporting the Model&lt;/strong&gt;: The &lt;code&gt;Task&lt;/code&gt; model is exported as the default export of this module. This allows  other parts of your application to import and use this model to create,  read, update, or delete tasks in your MongoDB database.&lt;/p&gt;

&lt;h3&gt;
  
  
  Build CRUD Endpoints
&lt;/h3&gt;

&lt;p&gt;In Next.js 13, setting up API routes is a straightforward process  that leverages the framework's file-based routing system. This approach  allows developers to create server-side endpoints directly within their  application structure, facilitating seamless integration between  frontend and backend code. &lt;/p&gt;

&lt;h3&gt;
  
  
  CRUD operations Flowchart
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://mermaid.live/edit#pako:eNqFktFugjAUhl-l6bXGe5YsEYpmicvMxJsVL5r2KERoTTlkWdR337HCxhaNvaHt-fqfD8qRa2eAR3xbuU9dKI8sE0-5ZTSmct2AZ1ONpbMbNh4_nxIPCoFlqtmfWCyXb6uMTdShnCDtNJvuYHxlnbWgkaFjIj6xRL46u3Mi7qkkUC-WemCXKOTlyRJXVXDt2rEisO-Arbds1WoNTUMCXbXT7RhlQhrVUzlPb_mlN_3-aM0AddHniFsWocYWZYPUiIChxPpgfj_TTC7XQ4tJVJreZPbYZJDFhNNtDRbvKF1R0_f9JyWggh-puRTpIs3SO17zx16DuEdeAaXbpIu129LX6rIghyCYWz7iNdBuaeg_PF7O5xwLqCHnEU2N8vuc5_ZMnGrRrb6s5hH6Fkbcu3ZX8GirqoZWbXh_UaqdV3WPHJT9cK7uoPM3zsvuIQ" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmermaid.ink%2Fimg%2Fpako%3AeNqFktFugjAUhl-l6bXGe5YsEYpmicvMxJsVL5r2KERoTTlkWdR337HCxhaNvaHt-fqfD8qRa2eAR3xbuU9dKI8sE0-5ZTSmct2AZ1ONpbMbNh4_nxIPCoFlqtmfWCyXb6uMTdShnCDtNJvuYHxlnbWgkaFjIj6xRL46u3Mi7qkkUC-WemCXKOTlyRJXVXDt2rEisO-Arbds1WoNTUMCXbXT7RhlQhrVUzlPb_mlN_3-aM0AddHniFsWocYWZYPUiIChxPpgfj_TTC7XQ4tJVJreZPbYZJDFhNNtDRbvKF1R0_f9JyWggh-puRTpIs3SO17zx16DuEdeAaXbpIu129LX6rIghyCYWz7iNdBuaeg_PF7O5xwLqCHnEU2N8vuc5_ZMnGrRrb6s5hH6Fkbcu3ZX8GirqoZWbXh_UaqdV3WPHJT9cK7uoPM3zsvuIQ%3Ftype%3Dpng" alt="crud flowchart" width="1352" height="606"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Breakdown of the flowchart
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;User Action&lt;/strong&gt;: The flowchart begins with a &lt;strong&gt;User Action&lt;/strong&gt; node, representing any interaction initiated by the user, such as creating, reading, updating, or deleting a task.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Create Task&lt;/strong&gt;: When the user decides to create a task, this action triggers a &lt;strong&gt;POST&lt;/strong&gt; request to the &lt;code&gt;/api/tasks&lt;/code&gt; endpoint. The system then connects to the &lt;strong&gt;MongoDB&lt;/strong&gt; database to perform the operation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Connecting to MongoDB&lt;/strong&gt;: The flow moves to establishing a connection with the MongoDB database. This connection is necessary for any database operations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Insert Task&lt;/strong&gt;: After connecting, the system inserts the new task into the &lt;strong&gt;Task Collection&lt;/strong&gt; within MongoDB. Once the task is successfully inserted, the system returns a success message back to the API route.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Read Tasks&lt;/strong&gt;: If the user wants to read or retrieve tasks, they initiate a &lt;strong&gt;GET&lt;/strong&gt; request to &lt;code&gt;/api/tasks&lt;/code&gt;. Similar to the create operation, this also connects to MongoDB.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fetch Tasks&lt;/strong&gt;: After establishing the connection, the system fetches all tasks from the &lt;strong&gt;Task Collection&lt;/strong&gt;. The retrieved list of tasks is then returned as a response to the client.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Update Task&lt;/strong&gt;: For updating an existing task, the user triggers a &lt;strong&gt;PUT&lt;/strong&gt; request to &lt;code&gt;/api/tasks/:id&lt;/code&gt;, where &lt;code&gt;:id&lt;/code&gt; represents the specific task's identifier. The system connects to MongoDB and updates the corresponding task document in the collection.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Return Updated Task&lt;/strong&gt;: After updating, it returns the updated task details back to the API route.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Delete Task&lt;/strong&gt;: To delete a task, the user initiates a &lt;strong&gt;DELETE&lt;/strong&gt; request to &lt;code&gt;/api/tasks/:id&lt;/code&gt;. As with previous operations, it connects to MongoDB and deletes the specified task document from the collection.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Return Deletion Confirmation&lt;/strong&gt;: Finally, after successfully deleting the task, it returns a confirmation message indicating that the deletion was successful.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Below, we will explore how API routes are set up in Next.js 13, the conventions surrounding file and folder naming.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;File Structure for API Routes&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;API routes in Next.js are defined within the &lt;code&gt;app&lt;/code&gt; directory, which is a new feature introduced in Next.js 13. This  directory structure enhances the way routes are organized compared to  previous versions.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Location&lt;/strong&gt;: API route files should be placed in the &lt;code&gt;app/api&lt;/code&gt; directory. For example, a file named &lt;code&gt;hello/route.js&lt;/code&gt; located at &lt;code&gt;app/api/hello/route.js&lt;/code&gt; would be accessible via the URL &lt;code&gt;/api/hello&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;File Naming&lt;/strong&gt;: The naming convention for API routes follows a clear pattern: Each API route is defined in a file named &lt;code&gt;route.js&lt;/code&gt; within its respective folder. This indicates that the folder corresponds to an endpoint. For dynamic routes, you can use square brackets in the folder name. For instance, &lt;code&gt;app/api/users/[id]/route.js&lt;/code&gt; would handle requests like &lt;code&gt;/api/users/1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Exporting Route Handlers&lt;/strong&gt;: Each API route file should export a default asynchronous function that handles incoming requests.&lt;/li&gt;
&lt;/ol&gt;

&lt;h5&gt;
  
  
  Handling Different HTTP Methods
&lt;/h5&gt;

&lt;p&gt;Next.js 13 allows you to define separate functions for handling  different HTTP methods (GET, POST, PUT, DELETE) within the same route  file&lt;/p&gt;

&lt;h4&gt;
  
  
  Create and Populate Collections with POST Requests
&lt;/h4&gt;

&lt;p&gt;A POST request is primarily used to send data to the server. This data can be included in the body of the request, allowing for the  transmission of various types of information, such as form submissions,  file uploads, or API interactions. We will use the POST request to create the collection in out case, &lt;code&gt;Tasks&lt;/code&gt; and send info about the &lt;code&gt;Tasks&lt;/code&gt; i.e &lt;code&gt;title&lt;/code&gt; and &lt;code&gt;description&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Lets create a folder &lt;code&gt;api&lt;/code&gt; in the &lt;code&gt;app&lt;/code&gt; folder. In the &lt;code&gt;api&lt;/code&gt; folder create a folder &lt;code&gt;tasks&lt;/code&gt; . Go ahead and create a file &lt;code&gt;route.js&lt;/code&gt; in the &lt;code&gt;tasks&lt;/code&gt; folder.  Add the code below to handle the POST requests.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Task&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@/app/lib/models/models&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;connectMongoDB&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@/app/lib/mongodbConnection&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;NextResponse&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;next/server&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;POST&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;description&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;connectMongoDB&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;       
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;Task&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;description&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;NextResponse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Task Created&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;201&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;NextResponse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Failed to create task&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Code explanation
&lt;/h3&gt;

&lt;p&gt;This code snippet is designed to handle incoming POST requests to  create a new task in the &lt;code&gt;MongoDB&lt;/code&gt; database. It utilises &lt;code&gt;Mongoose&lt;/code&gt; for  database interactions and the Next.js API routing capabilities. Here’s a breakdown of each part of the code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Task&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@/app/lib/models/models&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;connectMongoDB&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@/app/lib/mongodbConnection&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;NextResponse&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;next/server&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Imports&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Task&lt;/strong&gt;:  This imports the Mongoose model for the task, which defines the  structure of task documents in the MongoDB collection. It allows you to  interact with the tasks collection using Mongoose methods.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;connectMongoDB&lt;/strong&gt;: This imports a function that establishes a connection to the MongoDB  database. It ensures that the application can communicate with the  database before performing any operations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NextResponse&lt;/strong&gt;: Imported from &lt;code&gt;next/server&lt;/code&gt;, this utility is used to create responses for API requests, allowing you to return JSON responses easily.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;POST&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;POST(request)&lt;/strong&gt;: This line exports an asynchronous function named &lt;code&gt;POST&lt;/code&gt;, which handles incoming POST requests made to this API route. The function takes one parameter, &lt;code&gt;request&lt;/code&gt;, representing the HTTP request object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt; &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;description&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;request.json()&lt;/strong&gt;: Inside the &lt;code&gt;try&lt;/code&gt; block, this method attempts to parse the JSON body of the incoming  request. It reads the request body and converts it into a JavaScript  object. The destructuring assignment extracts &lt;code&gt;title&lt;/code&gt; and &lt;code&gt;description&lt;/code&gt;, which are expected fields for creating a new task.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;connectMongoDB&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;connectMongoDB()&lt;/strong&gt;: This function is called to establish a connection to the MongoDB  database. This step is crucial because any database operations (like  creating a new task) require an active connection.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;Task&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;description&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Task.create()&lt;/strong&gt;: The code calls this method with an object containing &lt;code&gt;title&lt;/code&gt; and &lt;code&gt;description&lt;/code&gt;. This method creates a new document in the tasks collection of the  MongoDB database using Mongoose. If successful, this operation adds a  new task with the specified title and description.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;NextResponse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Task Created&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;201&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Successful Response&lt;/strong&gt;: If all operations are successful, this line returns a JSON  response indicating that the task has been created. The response  includes a status code of 201 (Created), signifying that a new resource  has been successfully created on the server.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;   &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;NextResponse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Failed to create task&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Error Handling&lt;/strong&gt;: If any errors occur during parsing, connecting to MongoDB, or creating the task, they are caught by the &lt;code&gt;catch&lt;/code&gt; block. In case of an error, it returns a JSON response with an error message and a status code of 500 (Internal Server Error). This informs the client that something went wrong while processing their  request.&lt;/p&gt;

&lt;p&gt;We will proceed to test the API we have just created. We will use Postman for testing the API.&lt;/p&gt;

&lt;h3&gt;
  
  
  Set up Postman for API testing
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; &lt;strong&gt;Download and Install Postman&lt;/strong&gt;: Visit the &lt;a href="https://www.postman.com/downloads/" rel="noopener noreferrer"&gt;Postman website&lt;/a&gt; and download the version suitable for your operating system (Windows, mac OS, or Linux). Follow the installation instructions to set up Postman on your local machine.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; &lt;strong&gt;Create a Postman Account&lt;/strong&gt;: Open Postman and sign up for a free account if you  don’t already have one. This allows you to save your requests and  collections.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt;  &lt;strong&gt;Create a New Collection&lt;/strong&gt;: Click the &lt;strong&gt;New&lt;/strong&gt; button in the top left corner of the Postman window and select &lt;strong&gt;Collection&lt;/strong&gt;. Use the &lt;strong&gt;+&lt;/strong&gt; icon or the &lt;strong&gt;Create Collection&lt;/strong&gt; button to create a new collection that will hold all the various requests we will make. We choose to create a collection instead of individual HTTP Requests because it allows us to organize multiple types of requests (POST, GET, PUT, DELETE) in one place. This approach streamlines our workflow, making it easier to manage related requests and maintain consistency across our API testing efforts.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faw5winob26aurjjec029.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faw5winob26aurjjec029.png" alt="Postman homepage" width="800" height="401"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; &lt;strong&gt;Add Requests to Your Collection&lt;/strong&gt;: After creating your collection, click on the collection name, &lt;strong&gt;New Collection&lt;/strong&gt; in the left sidebar then hover over the name to reveal the three-dot menu (&lt;strong&gt;...&lt;/strong&gt;). Click in the three-dot menu (&lt;strong&gt;...&lt;/strong&gt;) and this will open a drop-down and choose &lt;strong&gt;Add request&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgsk3tnq3fl3ujq3dwv7f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgsk3tnq3fl3ujq3dwv7f.png" alt="Postman add request" width="800" height="401"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5:&lt;/strong&gt; &lt;strong&gt;Enter Request Details&lt;/strong&gt;: In the new request tab, enter the URL of your API endpoint &lt;code&gt;http://localhost:3000/api/tasks&lt;/code&gt;. Select the HTTP method (&lt;strong&gt;POST&lt;/strong&gt;) from the drop-down menu next to the URL field.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6d28i6eov5z3xcrv4m25.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6d28i6eov5z3xcrv4m25.png" alt="Request Details" width="800" height="401"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 6:&lt;/strong&gt; &lt;strong&gt;Set Up Request Body&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Switch to the &lt;strong&gt;Body&lt;/strong&gt; tab below the URL field.&lt;/li&gt;
&lt;li&gt;Select &lt;strong&gt;"raw"&lt;/strong&gt; and choose &lt;strong&gt;JSON&lt;/strong&gt; from the drop-down menu.&lt;/li&gt;
&lt;li&gt;Enter your JSON data in the body. For example:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"title"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Coding"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"description"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Write a program in Next JS that connects the backend to MongoDB."&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm60h84ygbmm46qdiww6g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm60h84ygbmm46qdiww6g.png" alt="Add details" width="800" height="401"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 7:&lt;/strong&gt; &lt;strong&gt;Send the Request&lt;/strong&gt;: Click the &lt;strong&gt;"Send"&lt;/strong&gt; button to submit your request. Review the response displayed in the lower section of Postman, which includes status codes, headers, and response body. You should get message in the repsonse section at the bottom and the status code should be  &lt;strong&gt;201&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"message"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Task Created"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqy0u94180gppxqjfy165.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqy0u94180gppxqjfy165.png" alt="Response page " width="800" height="401"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 8:&lt;/strong&gt; &lt;strong&gt;Change name of collection and request&lt;/strong&gt;: Click on the collection name in the left sidebar, and then hover over the collection to reveal the three-dot menu (...). Click on the Select "Rename" from the drop-down menu, enter the name &lt;strong&gt;nextjs-backend&lt;/strong&gt;, and press Enter to save the change. Next, locate the &lt;strong&gt;New Request&lt;/strong&gt; within that collection, hover over it to access the three-dot menu again, select "Rename," enter &lt;strong&gt;POST&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Furzvirjjt1zuo8r2gk80.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Furzvirjjt1zuo8r2gk80.png" alt="Name options" width="800" height="412"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd4l948vfp6v7w8n857oj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd4l948vfp6v7w8n857oj.png" alt="Rename collection" width="800" height="412"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 9:&lt;/strong&gt;  &lt;strong&gt;Save Your Request&lt;/strong&gt;: To save your request for future use, click on the &lt;strong&gt;"Save"&lt;/strong&gt; button. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fknzuhg2yamwkx1ffhu1g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fknzuhg2yamwkx1ffhu1g.png" alt="Saved request" width="800" height="412"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 10:&lt;/strong&gt;  Lets us create another task. Go back to step 6 and lets add another task with the details below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"title"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Next Js Docs"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"description"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Read the official next js documentation."&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  View Entries in MongoDB Atlas
&lt;/h2&gt;

&lt;p&gt;Let us verify if the task has been added to the database. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; &lt;strong&gt;Log into MongoDB Atlas&lt;/strong&gt;: Go to the &lt;a href="https://www.mongodb.com/cloud/atlas" rel="noopener noreferrer"&gt;MongoDB Atlas website&lt;/a&gt; and log in to your account.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; &lt;strong&gt;Select Your Project&lt;/strong&gt;: Once logged in, select the organization and project that contains your desired cluster from the navigation bar.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; &lt;strong&gt;Access Your Cluster&lt;/strong&gt;: Click on the &lt;strong&gt;"Clusters"&lt;/strong&gt; tab in the sidebar to view your clusters.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; &lt;strong&gt;Open the Collections Page&lt;/strong&gt;: Click the &lt;strong&gt;"Browse Collections"&lt;/strong&gt; button for your cluster. This action will take you to the Data Explorer, where you can view all databases and collections associated  with your cluster.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff2rsgqpgssboqb16pphl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff2rsgqpgssboqb16pphl.png" alt="Browse collections" width="800" height="414"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5:&lt;/strong&gt; &lt;strong&gt;Select Your Database&lt;/strong&gt;: In the left pane, find and click on the &lt;strong&gt;nextjs-backend&lt;/strong&gt; that contains the collection you created earlier.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 6:&lt;/strong&gt; &lt;strong&gt;View Your Collection&lt;/strong&gt;: After selecting the database, you will see a list of collections within it. Click on the &lt;strong&gt;tasks&lt;/strong&gt; which will then load the entry we just made like the one below. You will be able to view the two entries we made:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd0a0nbaxjvmchjdpjeub.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd0a0nbaxjvmchjdpjeub.png" alt="Database entries" width="800" height="417"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Retrieve All Entries from the Database with a GET Request
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;GET&lt;/strong&gt; request is a  fundamental HTTP method used to retrieve data from a specified resource  on a server. When you make a &lt;strong&gt;GET&lt;/strong&gt; request, you are essentially asking the server to send back information, such as web pages, images, or data in  formats like JSON or XML. This method is commonly used in web  development for fetching resources without altering the server's state,  making it safe . In our case, we will use the GET request  to retrieve all entries from the database, allowing us to display the  stored data and verify that our API is functioning correctly.&lt;/p&gt;

&lt;p&gt;Let us go back to &lt;code&gt;route.js&lt;/code&gt; file in the &lt;code&gt;tasks&lt;/code&gt; folder in the &lt;code&gt;app&lt;/code&gt; folder. Add the code below :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;GET&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;connectMongoDB&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;tasks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;Task&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;NextResponse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;tasks&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Error retrieving tasks:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;NextResponse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Failed to retrieve tasks&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Code explanation
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;GET&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;GET()&lt;/strong&gt;: This line exports an asynchronous function named &lt;code&gt;GET&lt;/code&gt;, which will handle incoming GET requests to this API route. It does not  take any parameters, as it is designed to fetch data without needing  additional input.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;try&lt;/strong&gt;: The code execution begins with a &lt;code&gt;try&lt;/code&gt; block, which allows for error handling. This block contains operations  that may potentially throw errors, such as connecting to the database  and querying for data&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;connectMongoDB&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;connectMongoDB()&lt;/strong&gt;: This function is called to establish a connection to the MongoDB  database. This step is crucial because any subsequent database  operations require an active connection. The &lt;code&gt;await&lt;/code&gt; keyword ensures that the function waits for the connection to be established before proceeding.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;tasks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;Task&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Task.find()&lt;/strong&gt;: The code then calls &lt;code&gt;Task.find()&lt;/code&gt;, which is a Mongoose method used to retrieve all documents from the &lt;code&gt;Task&lt;/code&gt; collection in the database. The result is stored in the &lt;code&gt;tasks&lt;/code&gt; variable. The &lt;code&gt;await&lt;/code&gt; keyword is used here to ensure that the function waits for the database query to complete before moving on.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;NextResponse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;tasks&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Returning JSON Response&lt;/strong&gt;:  If both the connection and data retrieval are successful, this line returns a JSON response containing the retrieved tasks. The &lt;code&gt;NextResponse.json()&lt;/code&gt; method formats the response as JSON and automatically sets the  appropriate content type. This allows clients (such as front-end  applications) to easily consume and display the data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;catch (error)&lt;/strong&gt;: If any errors occur during the execution of the code within the &lt;code&gt;try&lt;/code&gt; block, they will be caught by this &lt;code&gt;catch&lt;/code&gt; block.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Error retrieving tasks:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Logging Errors&lt;/strong&gt;: Inside the catch block, this line logs an error message to the  console along with the error details. This is useful for debugging  purposes, as it provides insight into what went wrong during execution&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;NextResponse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Failed to retrieve tasks&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Error Response&lt;/strong&gt;: If an  error occurs, a JSON response is returned indicating that there was a  failure in retrieving topics. The response includes an error message and sets the HTTP status code to 500 (Internal Server Error), signalling to  clients that something went wrong on the server side.&lt;/p&gt;

&lt;h3&gt;
  
  
  GET API Testing With Postman
&lt;/h3&gt;

&lt;p&gt;Now it is time to test if our API is working.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; &lt;strong&gt;Open Postman&lt;/strong&gt;: Launch the Postman application on your computer or open the Postman extension in Visual Studio Code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; &lt;strong&gt;Navigate to Your Collection&lt;/strong&gt;: In the left sidebar, find and click on the collection you previously created named &lt;strong&gt;nextjs-backend&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; &lt;strong&gt;Add a New Request&lt;/strong&gt;: Click on the three dots &lt;strong&gt;...&lt;/strong&gt; on the right side of the collection name. It will open a drop-down ad then choose  &lt;strong&gt;"Add request"&lt;/strong&gt; option to create a new request.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fej6i4u2h2wgcnrodobni.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fej6i4u2h2wgcnrodobni.png" alt="Add request" width="800" height="410"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; &lt;strong&gt;Set Up Your GET Request&lt;/strong&gt;: In the new request tab, enter the URL for your API  endpoint in the request URL field. Enter the URL: &lt;code&gt;http://localhost:3000/api/tasks&lt;/code&gt;. Select &lt;strong&gt;GET&lt;/strong&gt; as the HTTP method from the drop-down menu next to the URL field.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0sobsf4qjxidelgu5k0q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0sobsf4qjxidelgu5k0q.png" alt="Get request" width="800" height="410"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5:&lt;/strong&gt; &lt;strong&gt;Send the Request&lt;/strong&gt;: Click the &lt;strong&gt;"Send"&lt;/strong&gt; button to execute your GET request. Wait for a moment as Postman communicates with your API.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 6:&lt;/strong&gt; &lt;strong&gt;Review the Response&lt;/strong&gt;: After sending the request, check the response  section at the bottom of Postman. You should see a JSON response  containing all entries retrieved from your database under the &lt;code&gt;tasks&lt;/code&gt; key. If there are any errors, they will be displayed here as well, allowing you to troubleshoot if needed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F103zva4fee7hxtbnhkko.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F103zva4fee7hxtbnhkko.png" alt="All database entries" width="800" height="410"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 7:&lt;/strong&gt; &lt;strong&gt;Change name of collection and request&lt;/strong&gt;: Click on the &lt;strong&gt;New request&lt;/strong&gt; name in the left sidebar, and then hover over the collection to reveal the three-dot menu (⋮). Select "Rename" from the drop-down menu, enter the name &lt;strong&gt;GET&lt;/strong&gt;, and press Enter to save the change. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 8:&lt;/strong&gt; &lt;strong&gt;Save Your Request&lt;/strong&gt;: Click on the &lt;strong&gt;"Save"&lt;/strong&gt; button.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6bfjlli46xbvsygoif3m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6bfjlli46xbvsygoif3m.png" alt="Save response" width="800" height="410"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Remove Entries from the Database with DELETE Request
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;DELETE&lt;/strong&gt; request is  an HTTP method used to remove a specific resource from a server. When a  client sends a &lt;strong&gt;DELETE&lt;/strong&gt; request, it indicates the intention to permanently delete the resource identified by the specified URL. Unlike other  methods such as &lt;strong&gt;GET&lt;/strong&gt; or &lt;strong&gt;POST&lt;/strong&gt;, which are used for retrieving or creating  resources, the &lt;strong&gt;DELETE&lt;/strong&gt; method is specifically designed for deletion. In our application, we will use the &lt;strong&gt;DELETE&lt;/strong&gt; request to remove  entries from our database, such as deleting a specific topic or record.&lt;/p&gt;

&lt;p&gt;Let us go back to &lt;code&gt;route.js&lt;/code&gt; file in the &lt;code&gt;tasks&lt;/code&gt; folder in the &lt;code&gt;app&lt;/code&gt; folder. Add the code below :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;DELETE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;nextUrl&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;searchParams&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;connectMongoDB&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;Task&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findByIdAndDelete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;NextResponse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Task deleted&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Error deleting task:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;NextResponse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Failed to delete task&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Code explanation
&lt;/h3&gt;

&lt;p&gt;This code snippet defines a &lt;strong&gt;DELETE&lt;/strong&gt; request handler for removing a  specific task from a &lt;strong&gt;MongoDB&lt;/strong&gt; database in a Next.js API route. Below is a breakdown of each part of the code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;DELETE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;DELETE(request)&lt;/strong&gt;: This line exports an asynchronous function named &lt;code&gt;DELETE&lt;/code&gt;, which will handle incoming DELETE requests to this API route. It takes one parameter, &lt;code&gt;request&lt;/code&gt;, representing the HTTP request object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;try&lt;/strong&gt;: The code execution begins with a &lt;code&gt;try&lt;/code&gt; block, which allows for error handling. This block contains operations  that may potentially throw errors, such as connecting to the database  and deleting a task.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;nextUrl&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;searchParams&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;request.nextUrl.searchParams.get("id")&lt;/strong&gt;: This line extracts the ID of the task to be deleted from the query  parameters of the request URL. This allows clients to specify which task they want to delete by including its ID in the request URL&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;connectMongoDB&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;connectMongoDB()&lt;/strong&gt;: This  function is called to establish a connection to the MongoDB database.  This step is crucial because any subsequent database operations require  an active connection. The &lt;code&gt;await&lt;/code&gt; keyword ensures that the function waits for the connection to be established before proceeding&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;deletedTask&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;Task&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findByIdAndDelete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Task.findByIdAndDelete(id)&lt;/strong&gt;: The code attempts to delete the task with the specified ID using this  Mongoose method. It searches for a document by its ID and removes it  from the database. If successful, it assigns the deleted task's details  to &lt;code&gt;deletedTask&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;NextResponse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Task deleted&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Successful Deletion Response&lt;/strong&gt;: If the deletion was successful and a task was found, a JSON response is returned confirming that the task has been deleted, along with a status code of 200 (OK).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Error deleting task:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;catch (error)&lt;/strong&gt;: If any errors occur during execution (e.g., connection issues or unexpected errors), they are caught by this &lt;code&gt;catch&lt;/code&gt; block.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;NextResponse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Failed to delete task&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Error Response&lt;/strong&gt;: If an  error occurs, a JSON response is returned indicating that there was a  failure in deleting the task. The response includes an error message and sets the HTTP status code to 500 (Internal Server Error), signalling to  clients that something went wrong on the server side.&lt;/p&gt;

&lt;h3&gt;
  
  
  DELETE API Testing With Postman
&lt;/h3&gt;

&lt;p&gt;Now it is time to test if our API is working.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; &lt;strong&gt;Open Postman&lt;/strong&gt;: Launch the Postman application on your computer.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; &lt;strong&gt;Retrieve All Entries&lt;/strong&gt;: Navigate to the &lt;strong&gt;nextjs-backend&lt;/strong&gt; collection. Click on the previously saved &lt;strong&gt;GET&lt;/strong&gt; request that retrieves all entries from your database. Click the &lt;strong&gt;"Send"&lt;/strong&gt; button to execute the GET request. Review the response section at the bottom of Postman to see all entries returned from the database. Copy the ID of the  entry you wish to delete. We will copy the id of the first entry for this guide.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F80zkp4r1yptv29x9qo4g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F80zkp4r1yptv29x9qo4g.png" alt="First id" width="800" height="410"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; &lt;strong&gt;Add a New Request&lt;/strong&gt;: Click on the three dots &lt;strong&gt;...&lt;/strong&gt; on the right side of the collection name. It will open a drop-down ad then choose  &lt;strong&gt;"Add request"&lt;/strong&gt; option to create a new request.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; &lt;strong&gt;Set Up Your DELETE Request&lt;/strong&gt;: In the new request tab, enter the URL for your API  endpoint in the request URL field. Enter the URL: &lt;code&gt;http://localhost:3000/api/tasks&lt;/code&gt;. Select &lt;strong&gt;DELETE&lt;/strong&gt; as the HTTP method from the drop-down menu next to the URL field.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5:&lt;/strong&gt; Add query parameters. There are two ways to do this:&lt;/p&gt;

&lt;h4&gt;
  
  
  Option 1: Using Query Parameters
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Add Query Parameters&lt;/strong&gt;: Click on the &lt;strong&gt;"Params"&lt;/strong&gt; tab located below the URL field. In the key-value pairs section, enter a key (e.g., &lt;code&gt;id&lt;/code&gt;) and its corresponding value (e.g., &lt;code&gt;123&lt;/code&gt;), where &lt;code&gt;123&lt;/code&gt; is the ID of the task you wish to delete. This will append &lt;code&gt;?id=123&lt;/code&gt; to your URL. In our case we will use key value of &lt;strong&gt;id&lt;/strong&gt; and the value &lt;strong&gt;6775062f0058e4dab982b94&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fymmp1ik6a6rm1knuqpep.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fymmp1ik6a6rm1knuqpep.png" alt="Key-value pair" width="800" height="410"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Option 2: Using Path Parameters
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Using Path Parameters&lt;/strong&gt;: Alternatively, you can directly include the ID in the URL path. Modify your URL to look like this: &lt;code&gt;http://localhost:3000/api/tasks?id=123&lt;/code&gt;, where 123 is the ID of the task you wish to delete. In this case, you do not need to add any parameters in the &lt;strong&gt;Params&lt;/strong&gt; tab. In our case we will use the URL &lt;code&gt;http://localhost:3000/api/tasks?id=6775062f0058e4dab982b94&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftq5nol9b2idfiu3i48oz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftq5nol9b2idfiu3i48oz.png" alt="Id in url" width="800" height="410"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 6:&lt;/strong&gt; &lt;strong&gt;Send the Request&lt;/strong&gt;: Click the &lt;strong&gt;"Send"&lt;/strong&gt; button to execute your DELETE request. Wait for a moment as Postman communicates with your API.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 7:&lt;/strong&gt; &lt;strong&gt;Review the Response&lt;/strong&gt;: After sending the request, check the response  section at the bottom of Postman. &lt;br&gt;
If the task is successfully deleted, you should receive a JSON response similar to this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
   &lt;/span&gt;&lt;span class="nl"&gt;"message"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Task deleted"&lt;/span&gt;&lt;span class="w"&gt;
 &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The HTTP status code will typically be 200 OK, indicating that the request was processed successfully.&lt;/p&gt;

&lt;p&gt;If you attempt to delete a task that does not exist (e.g., using an ID that has already been deleted), you should receive a JSON response indicating that the task could not be found:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"error"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Task not found"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, the HTTP status code will be 404 Not Found, signalling that the resource you attempted to delete does not exist.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F24c4j72ydijsqf29xrfb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F24c4j72ydijsqf29xrfb.png" alt="Delete success" width="800" height="410"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 8:&lt;/strong&gt;  &lt;strong&gt;Change name of collection and request&lt;/strong&gt;: Click on the &lt;strong&gt;New request&lt;/strong&gt; name in the left sidebar, and then hover over the collection to reveal the three-dot menu (⋮). Select "Rename" from the drop-down menu, enter the name &lt;strong&gt;DELETE&lt;/strong&gt;, and press Enter to save the change. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcdndqrcsmm8jkuppy5g7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcdndqrcsmm8jkuppy5g7.png" alt="DELETE rename" width="800" height="410"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 9:&lt;/strong&gt; &lt;strong&gt;Save Your Request&lt;/strong&gt;: click on the &lt;strong&gt;"Save"&lt;/strong&gt; button.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flb6b0s6wfd6up5qzcv1x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flb6b0s6wfd6up5qzcv1x.png" alt="Save delete" width="800" height="410"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Viewing Entries in MongoDB Atlas
&lt;/h2&gt;

&lt;p&gt;Let us verify if the task has been deleted to the database. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; &lt;strong&gt;Log into MongoDB Atlas&lt;/strong&gt;: Go to the &lt;a href="https://www.mongodb.com/cloud/atlas" rel="noopener noreferrer"&gt;MongoDB Atlas website&lt;/a&gt; and log in to your account.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; &lt;strong&gt;Select Your Project&lt;/strong&gt;: Once logged in, select the organization and project that contains your desired cluster from the navigation bar.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; &lt;strong&gt;Access Your Cluster&lt;/strong&gt;: Click on the &lt;strong&gt;"Clusters"&lt;/strong&gt; tab in the sidebar to view your clusters.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; &lt;strong&gt;Open the Collections Page&lt;/strong&gt;: Click the &lt;strong&gt;"Browse Collections"&lt;/strong&gt; button for your cluster. This action will take you to the Data Explorer, where you can view all databases and collections associated  with your cluster.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fru0l1wzq60rapk3hbtns.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fru0l1wzq60rapk3hbtns.png" alt="Browse collections" width="800" height="414"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5:&lt;/strong&gt;  &lt;strong&gt;Select Your Database&lt;/strong&gt;: In the left pane, find and click on the &lt;strong&gt;nextjs-backend&lt;/strong&gt; that contains the collection you created earlier.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 6:&lt;/strong&gt; &lt;strong&gt;View Your Collection&lt;/strong&gt;: After selecting the database, you will see a list of collections within it. Click on the &lt;strong&gt;tasks&lt;/strong&gt; which will then load the remaining entries after deletion. There should be one entry because we delete the first entry:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftuc3v57xru40roozq2hs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftuc3v57xru40roozq2hs.png" alt="View entries" width="800" height="416"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Update Entries in the Database with a PUT Request
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;PUT&lt;/strong&gt; request is an HTTP method used primarily to update an existing resource on the server. When you send a &lt;strong&gt;PUT&lt;/strong&gt; request, you are instructing the  server to completely replace the current representation of the resource  with the new data provided in the request body. This means that all  fields of the resource must be included in the request. In our application, we will use the &lt;strong&gt;PUT&lt;/strong&gt; request to update the  details of an entry in our database, such as modifying a task's title,  description. By implementing dynamic routing, we can specify  which entry to update by including its &lt;strong&gt;ID&lt;/strong&gt; in the  URL. For example, a &lt;strong&gt;PUT&lt;/strong&gt; request might look like &lt;code&gt;http://localhost:3000/api/tasks/123&lt;/code&gt;, where &lt;code&gt;123&lt;/code&gt; is the ID of the task we want to update. The request body will contain  the new data in JSON format, and upon successful execution, the server  will respond with a confirmation message indicating that the update was  successful. &lt;/p&gt;

&lt;p&gt;To implement the PUT request for updating a task in a Next.js API route, you should create a folder named &lt;code&gt;[id]&lt;/code&gt; within the &lt;code&gt;/app/api/tasks/&lt;/code&gt; directory. Inside this folder, create a file named &lt;code&gt;route.js&lt;/code&gt;. The naming of the folder as &lt;code&gt;[id]&lt;/code&gt; indicates that it will handle dynamic routing based on the task ID,  allowing the API to capture which specific task is being updated through the URL. This structure enables you to define a clear and organised  endpoint, such as &lt;code&gt;http://localhost:3000/api/tasks/123&lt;/code&gt;, where &lt;code&gt;123&lt;/code&gt; represents the ID of the task to be updated. By using square brackets,  Next.js recognizes that this folder is meant for dynamic segments,  facilitating the handling of various tasks without needing separate  files for each ID.&lt;br&gt;
Below is how the folder structure should look like:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzdftd9retkrgydbuv0sa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzdftd9retkrgydbuv0sa.png" alt="Folder structure" width="298" height="587"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Inside the &lt;code&gt;route.js&lt;/code&gt; write the following code :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;PUT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;params&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;newTitle&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;newDescription&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;description&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;connectMongoDB&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;Task&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findByIdAndUpdate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;description&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;new&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;NextResponse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Task updated successfully&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Error updating task:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;NextResponse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Failed to update task&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Code explanation
&lt;/h3&gt;

&lt;p&gt;This code snippet defines a &lt;strong&gt;PUT&lt;/strong&gt; request handler that allows  clients to update an existing task in the database. Below is a breakdown of each part of the code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;PUT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;params&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;PUT(request, { params })&lt;/strong&gt;: This line exports an asynchronous function named &lt;code&gt;PUT&lt;/code&gt;, which will handle incoming PUT requests to this API route. It takes two parameters: &lt;code&gt;request&lt;/code&gt;, representing the HTTP request object, and &lt;code&gt;params&lt;/code&gt;, which contains dynamic route parameters (in this case, the task ID).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;try&lt;/strong&gt;: The execution begins with a &lt;code&gt;try&lt;/code&gt; block that allows for error handling. This block contains operations  that may potentially throw errors, such as connecting to the database  and updating a task.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;const { id } = params;&lt;/strong&gt;: The ID of the task to be updated is extracted from the &lt;code&gt;params&lt;/code&gt; object. This ID is dynamically captured from the URL when the request  is made, allowing the API to identify which task needs to be updated.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;newTitle&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;newDescription&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;description&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;await request.json()&lt;/strong&gt;: The new title and description for the task are retrieved from the  request body. The properties are renamed using destructuring assignment  for clarity, where &lt;code&gt;newTitle&lt;/code&gt; becomes &lt;code&gt;title&lt;/code&gt; and &lt;code&gt;newDescription&lt;/code&gt; becomes &lt;code&gt;description&lt;/code&gt;. This makes it clear what data is being used in the update operation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;connectMongoDB&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;connectMongoDB()&lt;/strong&gt;: This function is called to establish a connection to the MongoDB  database. This step is essential for performing any database operations. The &lt;code&gt;await&lt;/code&gt; keyword ensures that the function waits for the connection to be established before proceeding.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;Task&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findByIdAndUpdate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;description&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;new&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Task.findByIdAndUpdate(id, { title, description }, { new: true })&lt;/strong&gt;: The code attempts to update the task using this Mongoose method. It  searches for a document by its ID and updates it with the new title and  description. The &lt;code&gt;{ new: true }&lt;/code&gt; option ensures that the updated document is returned after the operation, allowing you to see what was changed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;NextResponse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Task updated successfully&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Successful Update Response&lt;/strong&gt;: If the update operation is successful, a JSON response is returned  confirming that the task has been updated successfully, along with a  status code of 200 (OK).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Catch Block&lt;/strong&gt;: If any  errors occur during execution (e.g., connection issues or if no task is  found with the provided ID), they are caught by this &lt;code&gt;catch&lt;/code&gt; block.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Error updating task:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Logging Errors&lt;/strong&gt;: Inside the catch block, an error message is logged to the console along with  the error details. This is useful for debugging purposes, providing  insight into what went wrong during execution.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;NextResponse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Failed to update task&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Error Response&lt;/strong&gt;: If an  error occurs during the update process, a JSON response is returned  indicating that there was a failure in updating the task. The response  includes an error message and sets the HTTP status code to 500 (Internal Server Error), signalling to clients that something went wrong on the  server side.&lt;/p&gt;

&lt;h3&gt;
  
  
  PUT API Testing With Postman
&lt;/h3&gt;

&lt;p&gt;Now it is time to test if our API is working.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; &lt;strong&gt;Open Postman&lt;/strong&gt;: Launch the Postman application on your computer.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; &lt;strong&gt;Retrieve All Entries&lt;/strong&gt;: Navigate to the &lt;strong&gt;nextjs-backend&lt;/strong&gt; collection. Click on the previously saved &lt;strong&gt;GET&lt;/strong&gt; request that retrieves all entries from your database. Click the &lt;strong&gt;"Send"&lt;/strong&gt; button to execute the GET request. Review the response section at the bottom of Postman to see all entries returned from the database. Copy the ID of the  entry you wish to delete. We will copy the id of the first entry for this guide.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3skcnxggy548zu95w349.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3skcnxggy548zu95w349.png" alt="All entries" width="800" height="390"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; &lt;strong&gt;Create a New PUT Request&lt;/strong&gt;: Hover over the &lt;strong&gt;nextjs-backend&lt;/strong&gt; collection name in the left sidebar until three dots appear next to it. Click on these three dots to open a drop-down menu, then choose the &lt;strong&gt;"Add request"&lt;/strong&gt; option to create a new request.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmqwbvjvzhfh08rkzvj7p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmqwbvjvzhfh08rkzvj7p.png" alt="Add request" width="800" height="390"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; &lt;strong&gt;Enter the URL for Your PUT API Endpoint&lt;/strong&gt;: In the new request tab, enter the URL for your PUT API endpoint. For  example, if you want to update a task with a specific ID, your URL might look like this: &lt;code&gt;http://localhost:3000/api/tasks/[id]&lt;/code&gt;, where &lt;code&gt;[id]&lt;/code&gt; is the ID of the task you copied earlier. In our case it will be &lt;code&gt;http://localhost:3000/api/tasks/677507210058e4dab982b943&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5:&lt;/strong&gt; &lt;strong&gt;Select HTTP Method&lt;/strong&gt;: Choose &lt;strong&gt;PUT&lt;/strong&gt; from the drop-down menu next to the URL field.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5fjhvy0j83bjo4oetsqs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5fjhvy0j83bjo4oetsqs.png" alt="PUT url" width="800" height="390"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 6:&lt;/strong&gt; &lt;strong&gt;Set Up the Request Body&lt;/strong&gt;: Click on the &lt;strong&gt;"Body"&lt;/strong&gt; tab below the URL field, select &lt;strong&gt;raw&lt;/strong&gt;, and choose &lt;strong&gt;JSON&lt;/strong&gt; from the drop-down menu that appears next to it. Enter the JSON data you want to update, for example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"newTitle"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Updated Next Js Docs "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"newDescription"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;"Read the updated official next js documentation."&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjzj0tg55jds5aowoy4my.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjzj0tg55jds5aowoy4my.png" alt="Updated entry" width="800" height="390"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 7:&lt;/strong&gt; &lt;strong&gt;Send the Request&lt;/strong&gt;: Click the &lt;strong&gt;"Send"&lt;/strong&gt; button to execute your PUT request and wait for a moment as Postman communicates with your API.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 8:&lt;/strong&gt; &lt;strong&gt;Review the Response&lt;/strong&gt;:  After sending the request, check the response section at the bottom of  Postman. You should see a JSON response indicating whether the task has  been updated successfully, such as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"message"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Task updated successfully"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If there was an issue (e.g., if no task was found with that ID), you might see an error message like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"error"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Task not found"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fizx488c50paaa4b2gpjs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fizx488c50paaa4b2gpjs.png" alt="Updated Entry" width="800" height="390"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 9:&lt;/strong&gt; &lt;strong&gt;Change name of collection and request&lt;/strong&gt;: Click on the &lt;strong&gt;New request&lt;/strong&gt; name in the left sidebar, and then hover over the collection to reveal the three-dot menu &lt;strong&gt;(...)&lt;/strong&gt;. Select "Rename" from the drop-down menu, enter the name &lt;strong&gt;PUT&lt;/strong&gt;, and press Enter to save the change. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc6zzd2gkhw3eb36xgvea.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc6zzd2gkhw3eb36xgvea.png" alt="Rename PUT" width="800" height="390"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 10:&lt;/strong&gt; &lt;strong&gt;Save Your Request&lt;/strong&gt;: Click on the &lt;strong&gt;"Save"&lt;/strong&gt; button.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbcq0flop2qbyqsdr59fd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbcq0flop2qbyqsdr59fd.png" alt="Save put" width="800" height="390"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  View Entries in MongoDB Atlas
&lt;/h2&gt;

&lt;p&gt;Let us verify if the task details have been updated in the database. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; &lt;strong&gt;Log into MongoDB Atlas&lt;/strong&gt;: Go to the &lt;a href="https://www.mongodb.com/cloud/atlas" rel="noopener noreferrer"&gt;MongoDB Atlas website&lt;/a&gt; and log in to your account.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; &lt;strong&gt;Select Your Project&lt;/strong&gt;: Once logged in, select the organization and project that contains your desired cluster from the navigation bar.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; &lt;strong&gt;Access Your Cluster&lt;/strong&gt;: Click on the &lt;strong&gt;"Clusters"&lt;/strong&gt; tab in the sidebar to view your clusters.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; &lt;strong&gt;Open the Collections Page&lt;/strong&gt;: Click the &lt;strong&gt;"Browse Collections"&lt;/strong&gt; button for your cluster. This action will take you to the Data Explorer, where you can view all databases and collections associated  with your cluster.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmfickqep4qonxsyeodql.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmfickqep4qonxsyeodql.png" alt="Browse collections" width="800" height="414"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5:&lt;/strong&gt; &lt;strong&gt;Select Your Database&lt;/strong&gt;: In the left pane, find and click on the &lt;strong&gt;nextjs-backend&lt;/strong&gt; that contains the collection you created earlier.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 6:&lt;/strong&gt; &lt;strong&gt;View Your Collection&lt;/strong&gt;: After selecting the database, you will see a list of collections within it. Click on the &lt;strong&gt;tasks&lt;/strong&gt; which will then load the remaining entries after deletion. There title and description should be updated to :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tex"&gt;&lt;code&gt;"title": "Updated Next Js Docs ",
"description":"Read the updated official next js documentation."
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frjiwe3xyb5ynq45j8uaw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frjiwe3xyb5ynq45j8uaw.png" alt="Database update" width="800" height="414"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion and Next Steps
&lt;/h3&gt;

&lt;p&gt;In conclusion, you have successfully set up a robust Next.js application integrated with MongoDB, enabling essential CRUD (Create, Read, Update, Delete) operations on your data. This foundational setup lays the groundwork for more complex applications. You've learned to create dynamic API routes that facilitate interaction with your database and effectively manage tasks. Utilizing Postman for testing has also provided valuable experience in verifying the functionality of your endpoints. As you continue your development journey, stay tuned for the next steps where I will demonstrate how to seamlessly combine the backend with the front end. We’ll enhance user experience and functionality while incorporating styles to create a polished application. This knowledge equips you with the skills necessary to build scalable web applications and explore advanced features in both Next.js and MongoDB. &lt;br&gt;
I also want to share that my journey into JavaScript and frameworks like Next.js began at &lt;a href="https://www.zone01kisumu.ke/" rel="noopener noreferrer"&gt;Zone01 Kisumu&lt;/a&gt;, where I am an apprentice software developer. The hands-on experience there has been invaluable as I continue to grow in this field. Exciting developments are ahead, so keep following along! Happy coding! This version retains the key messages while being more concise, making it easier for readers to digest&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>mongodb</category>
      <category>api</category>
      <category>backenddevelopment</category>
    </item>
  </channel>
</rss>
