Mid
From PDF
Node.js
Node.js
How do you handle database errors in Node.js?
Short answer: Use try/catch blocks with async/await to catch exceptions. Handle errors in callbacks or promise .catch() when using promise-based APIs. Log errors for debugging. Return meaningful error messages to the client without exposing sensitive info. Example: try { const user = await User.findById(id);
Example code
if (!user) throw new Error('User not found'); } catch (error) { console.error(error); res.status(500).send('Something went wrong'); }
Real-world example (ShopNest)
A ShopNest BFF in Node.js aggregates catalog and price APIs for the React storefront.
Say this in the interview
- Define — one clear sentence (the short answer above).
- Example — relate it to a project like ShopNest or your real work.
- Trade-off — when you would not use it.
Share this Q&A
Share preview image: https://www.toolliyo.com/images/toolliyo-logo.png