Skip to content

Refactor MongoDB connection to use async/await#55

Merged
gbowne1 merged 2 commits intogbowne1:masterfrom
rupcode1794:refactor-mongo-async-await
Feb 8, 2026
Merged

Refactor MongoDB connection to use async/await#55
gbowne1 merged 2 commits intogbowne1:masterfrom
rupcode1794:refactor-mongo-async-await

Conversation

@rupcode1794
Copy link
Contributor

Title

Refactor MongoDB connection to use async/await

Description

This PR refactors the MongoDB connection in server.js to use async/await for cleaner asynchronous handling. The change improves error handling and aligns with recommended best practices.

Changes Made

  • Converted mongoose.connect(MONGODB_URI) to an async function with try/catch
  • Removed the previous .then().catch() approach
  • No changes were made to other routes or logic

Testing

  • ✅ Server runs successfully on http://localhost:3000
  • ✅ MongoDB connection established successfully (🟢 MongoDB connected successfully)
  • ✅ Landing page loads correctly
  • /api/streams endpoint returns expected JSON data

Screenshots / Logs / Pastebin

  • Terminal log: Pastebin link (expires in 7 days)
  • Landing page screenshot:
    Landing Page - DevStream API
  • /api/streams endpoint response screenshot:
    API Streams Endpoint Response

Code Changes

// Before
mongoose.connect(MONGODB_URI)
    .then(() => console.log('🟢 MongoDB connected successfully.'))
    .catch(err => console.error('🔴 MongoDB connection error:', err));

// After
try {
    await mongoose.connect(MONGODB_URI);
    console.log('🟢 MongoDB connected successfully.');
} catch (err) {
    console.error('🔴 MongoDB connection error:', err);
}

@gbowne1 gbowne1 linked an issue Jan 25, 2026 that may be closed by this pull request
@gbowne1 gbowne1 added enhancement New feature or request help wanted Extra attention is needed labels Jan 25, 2026
Copy link
Owner

@gbowne1 gbowne1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rupcode1794

I checked out this change locally for testing and review for merge

I started the server and it appears to work properly now with an async await

It also now works as expected per the issue and the PR descriptions.

Approving this PR for merge pending further review by collaborators and other maintainers.

Thanks for the opportunity to review your PR and for your contribution to this project.

Please feel free to work on this projects other issues and or any of the other issues in my other projects in my GitHub https://github.com/gbowne1/ especially since you do MERN https://github.com/gbowne1/reactsocialnetwork https://github.com/gbowne1/codebooker https://github.com/gbowne1/SavvyBudget https://github.com/gbowne1/fashion-marketplace/ and theres a few others

Thanks

@gbowne1
Copy link
Owner

gbowne1 commented Jan 28, 2026

@rupcode1794

I already reviewed and approved this PR. Am now waiting on other collaborators to review this PR for some measure of safety while we wait for unit tests and ci to be built for this

I'll merge this after at least one or two other collaborators and maintainers approved this PR.

Feel free to checkout rebase and review other PRs for approval

Thanks

Copy link
Owner

@gbowne1 gbowne1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has already been reviewed by myself and accepted for merge now awaiting further review by other collaborators and maintainers and contributors to this project

Will merge this after it's been reviewed and accepted by a minimum of one or two others

This looks great.

Approving this PR for merge pending further review

@shishir-21
Copy link
Collaborator

@gbowne1

One small suggestion (optional)

  • If the project grows, the DB connection logic could be moved into a separate utility/module for better structure and reuse.

Copy link
Collaborator

@shishir-21 shishir-21 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rupcode1794

Thanks for the PR

This PR resolves Issue #47 by switching the MongoDB connection to async/await.
The code is cleaner and easier to read, with no functional changes.

Overall, this PR looks good to me and is ready to merge 🚀

@gbowne1 gbowne1 requested a review from Ved178 January 30, 2026 19:35
@gbowne1
Copy link
Owner

gbowne1 commented Jan 31, 2026

Well, now this has minor conflicts. Should be okay. Rebase then apply? Or..

@rupcode1794
Copy link
Contributor Author

@gbowne1 Noted! I'll resolve the conflicts and update the PR. Need some time to rebase and test. Will update soon. ✅

@gbowne1
Copy link
Owner

gbowne1 commented Jan 31, 2026

@rupcode1794

Several incoming merges so be sure your PR will merge after each

@gbowne1
Copy link
Owner

gbowne1 commented Feb 6, 2026

@glenjaysondmello @Ved178 could you please review this.

I'll merge. I am inclined to keep both changes for all of the server js merge conflicts it has now using the merge conflict resolve button her on GitHub.

Copy link
Collaborator

@glenjaysondmello glenjaysondmello left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Reviewed and tested locally.

The async/await MongoDB connection works correctly and error handling behaves as expected. No functional regressions observed.

Looks good to merge. 👍

@gbowne1
Copy link
Owner

gbowne1 commented Feb 6, 2026

@glenjaysondmello

This has merge conflict's with server.js it might cause issues with #87

I want to merge 87 ahead of this then just accept both changes in the merge conflict UI on here (tad easier than doing the workaround)

Thoughts?

@gbowne1
Copy link
Owner

gbowne1 commented Feb 8, 2026

Merging this as is after successful approval of reviews post merge of #87

@gbowne1 gbowne1 merged commit 3d30c0c into gbowne1:master Feb 8, 2026
0 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request help wanted Extra attention is needed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[TODO] use async/await for mongo connection

4 participants