Challenge - 5 Problems
Database Integration Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate1:30remaining
Why use a database with Express?
Why is integrating a database important when building an Express app?
Attempts:
2 left
💡 Hint
Think about what happens to data when the server restarts.
✗ Incorrect
Databases keep data safe even if the server stops or restarts. Express alone does not save data permanently.
❓ component_behavior
intermediate1:30remaining
What happens when Express app lacks database integration?
Consider an Express app that stores user data only in variables inside the server code. What happens when the server restarts?
Attempts:
2 left
💡 Hint
Think about where variables live and what happens on server restart.
✗ Incorrect
Variables in server code exist only while the server runs. Restarting clears them, so data is lost without a database.
📝 Syntax
advanced2:00remaining
Identify the correct way to connect Express with a MongoDB database
Which code snippet correctly connects an Express app to MongoDB using the official MongoDB Node.js driver?
Attempts:
2 left
💡 Hint
Check for correct import syntax, connection string, and method names.
✗ Incorrect
Option A uses ES module import, correct connection string, awaits connection, and uses correct db method.
❓ state_output
advanced1:30remaining
What is the output when querying a database in Express?
Given this Express route using async/await to fetch users from a database, what will be sent to the client?
```js
app.get('/users', async (req, res) => {
const users = await db.collection('users').find().toArray();
res.json(users);
});
```
Attempts:
2 left
💡 Hint
What does toArray() do on a MongoDB cursor?
✗ Incorrect
toArray() converts the cursor to an array of documents, which is sent as JSON to the client.
🔧 Debug
expert2:30remaining
Why does this Express app fail to save data to the database?
Examine the code below. Why does the new user not get saved to the database?
```js
app.post('/add-user', (req, res) => {
const user = req.body;
db.collection('users').insertOne(user);
res.send('User added');
});
```
Attempts:
2 left
💡 Hint
Does Express parse JSON request bodies automatically?
✗ Incorrect
Express does not parse JSON bodies by default. Without app.use(express.json()), req.body is undefined, causing insertOne(undefined) to throw an error synchronously.