0
0
Node.jsframework~20 mins

Why database connectivity matters in Node.js - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Database Connectivity Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why is database connectivity important in Node.js applications?

Choose the best reason why connecting to a database is crucial for most Node.js apps.

AIt automatically fixes bugs in the app without developer input.
BIt speeds up the app by running all code inside the database server.
CIt allows the app to store and retrieve data dynamically, making it interactive and useful.
DIt replaces the need for any server-side code in the app.
Attempts:
2 left
💡 Hint

Think about what apps do with user information or content they show.

component_behavior
intermediate
2:00remaining
What happens if a Node.js app fails to connect to its database?

Consider a Node.js app that tries to fetch user data but the database connection fails. What will likely happen?

AThe app will crash or show an error because it can't get the needed data.
BThe app will continue normally and show cached data automatically.
CThe app will switch to a backup database without any code changes.
DThe app will run faster because it skips database calls.
Attempts:
2 left
💡 Hint

Think about what happens when the app asks for data but none arrives.

📝 Syntax
advanced
2:30remaining
Identify the correct way to connect to a MongoDB database using Node.js and the official driver.

Which code snippet correctly connects to a MongoDB database using the mongodb package?

Node.js
const { MongoClient } = require('mongodb');
const uri = 'mongodb://localhost:27017';

async function connect() {
  // connection code here
}
A
const client = MongoClient(uri);
client.connect();
console.log('Connected');
B
const client = new MongoClient(uri);
await client.connect();
console.log('Connected');
C
const client = new MongoClient();
await client.connect(uri);
console.log('Connected');
D
const client = new MongoClient(uri);
client.connect(() => console.log('Connected'));
Attempts:
2 left
💡 Hint

Remember to create a new MongoClient with the URI and use await to connect.

state_output
advanced
2:00remaining
What will be logged after this Node.js database connection code runs?

Consider this code snippet using pg package for PostgreSQL:

const { Client } = require('pg');
const client = new Client({ connectionString: 'postgresql://user:pass@localhost/db' });

async function run() {
  await client.connect();
  const res = await client.query('SELECT 1 AS number');
  console.log(res.rows[0].number);
  await client.end();
}

run();

What will be printed to the console?

Anull
Bundefined
CError: connection refused
D1
Attempts:
2 left
💡 Hint

The query selects a constant number 1 as 'number'.

🔧 Debug
expert
3:00remaining
Why does this Node.js code throw a 'TypeError: client.query is not a function'?

Examine the code below:

const { Client } = require('pg');
const client = Client({ connectionString: 'postgresql://user:pass@localhost/db' });

async function run() {
  await client.connect();
  const res = await client.query('SELECT NOW()');
  console.log(res.rows[0]);
  await client.end();
}

run();

Why does it throw the error?

ABecause <code>Client</code> was called without <code>new</code>, so <code>client</code> is undefined and has no <code>query</code> method.
BBecause the connection string is invalid and causes <code>query</code> to fail.
CBecause <code>await</code> cannot be used outside an async function.
DBecause <code>client.end()</code> is missing a callback function.
Attempts:
2 left
💡 Hint

Check how the Client object is created.