Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the pg module.
Node.js
const { Client } = require('[1]'); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'express' instead of 'pg'.
Using 'mysql' which is for a different database.
✗ Incorrect
You need to import the pg module to use PostgreSQL in Node.js.
2fill in blank
mediumComplete the code to create a new client with connection details.
Node.js
const client = new Client({ connectionString: '[1]' }); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using HTTP or FTP URLs instead of PostgreSQL connection string.
Omitting username or database name.
✗ Incorrect
The connection string must start with postgresql:// and include user, password, host, port, and database name.
3fill in blank
hardFix the error in the code to connect the client asynchronously.
Node.js
await client.[1](); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'disconnect' instead of 'connect'.
Using non-existent methods like 'start' or 'open'.
✗ Incorrect
The method to start the connection is connect(), and it returns a promise.
4fill in blank
hardFill both blanks to query the database and get results.
Node.js
const res = await client.[1]('[2]');
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'connect' or 'disconnect' instead of 'query'.
Using invalid SQL commands.
✗ Incorrect
Use query method with a SQL string to get data from the database.
5fill in blank
hardFill all three blanks to close the client connection properly.
Node.js
try { await client.[1](); // do queries } finally { await client.[2](); console.[3]('Connection closed'); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'start' instead of 'connect'.
Using 'disconnect' before queries.
Using 'console.error' instead of 'console.log'.
✗ Incorrect
You connect first, then disconnect in finally block, and log a message with console.log.