šŸš€ LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
šŸŽ“ COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
HTML MASTER CLASS /// LEARN TAGS /// BUILD STRUCTURE /// SEMANTIC WEB /// HTML MASTER CLASS /// LEARN TAGS ///

Untitled Lesson

⚔ Total XP: 0|šŸ’» backend XP: 0

Skill Matrix

UNLOCK NODES BY LEARNING NEW TAGS.

Select an unlocked node to view details root

Pascual Vila

Pascual Vila

Frontend Instructor // Code Syllabus

Common Pitfalls & Errors

The Error //

Opening a new database connection (or pool) inside every request handler

// Wrong: new connection per request app.get('/users', async (req, res) => { const pool = new Pool({ connectionString: process.env.PG_URI }); res.json(await pool.query('SELECT * FROM users')); }); // Correct: one pool, created once at startup // config/db.js export const pool = new Pool({ connectionString: process.env.PG_URI, max: 10 });

The Solution //

Calling mongoose.connect() or new Pool() inside a route handler establishes an expensive new connection on every single request instead of reusing one, quickly exhausting the database's max connection limit under real traffic and crashing the server. Create the connection/pool exactly once at application startup, in a dedicated config/db module, and import that single instance everywhere it's needed.

The Error //

Forgetting to await a database call and treating the resulting Promise as the data

// Wrong: user is a pending Promise, not the record const user = User.findById(id); console.log(user.name); // undefined / crash // Correct const user = await User.findById(id); console.log(user.name);

The Solution //

Every Mongoose/Prisma/pg call returns a Promise because it involves network I/O to a separate process; omitting await means the variable holds a pending Promise object instead of the actual rows, and any property access on it (like user.name) throws or silently returns undefined.

Continue Learning