0
0
Node.jsframework~10 mins

MySQL connection with mysql2 in Node.js - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the mysql2 package.

Node.js
const mysql = require('[1]');
Drag options to blanks, or click blank then click option'
Amongodb
Bmysql
Cpg
Dmysql2
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'mysql' instead of 'mysql2' in require statement.
Using unrelated package names like 'pg' or 'mongodb'.
2fill in blank
medium

Complete the code to create a connection using mysql2.

Node.js
const connection = mysql.createConnection({ host: 'localhost', user: 'root', password: '[1]', database: 'testdb' });
Drag options to blanks, or click blank then click option'
Apassword123
Broot
Clocalhost
Dtestdb
Attempts:
3 left
💡 Hint
Common Mistakes
Putting the username or host in the password field.
Leaving the password empty or incorrect.
3fill in blank
hard

Fix the error in the code to connect to the database.

Node.js
connection.[1](err => { if (err) throw err; console.log('Connected!'); });
Drag options to blanks, or click blank then click option'
AconnectTo
Bstart
Cconnect
Dopen
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect method names like 'connectTo' or 'open'.
Not calling the connect method at all.
4fill in blank
hard

Fill both blanks to query the database and handle the result.

Node.js
connection.query('SELECT * FROM users WHERE id = ?', [[1]], (err, [2]) => { if (err) throw err; console.log(results); });
Drag options to blanks, or click blank then click option'
AuserId
Bresults
Cerror
Ddata
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'error' or 'data' instead of 'results' for the callback parameter.
Using a wrong variable name for the query parameter.
5fill in blank
hard

Fill all three blanks to close the connection after the query.

Node.js
connection.query('SELECT * FROM products', (err, results) => { if (err) throw err; console.log(results); connection.[1](() => { console.log('[2]'); process.[3](); }); });
Drag options to blanks, or click blank then click option'
Aend
BConnection closed
Cexit
Dclose
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'close' instead of 'end' to close the connection.
Not calling process.exit() to end the Node.js process.