0
0
Expressframework~10 mins

Why database integration matters in Express - Test Your Understanding

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

Complete the code to import the Express module.

Express
const express = require('[1]');
Drag options to blanks, or click blank then click option'
Ahttp
Bfs
Cpath
Dexpress
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'http' instead of 'express' to import the framework.
Forgetting to put the module name in quotes.
2fill in blank
medium

Complete the code to connect to a MongoDB database using Mongoose.

Express
const mongoose = require('mongoose');
mongoose.connect('[1]');
Drag options to blanks, or click blank then click option'
A'http://localhost:3000'
B'mongodb://localhost:27017/mydb'
C'express://localhost:27017'
D'file://mydb'
Attempts:
3 left
💡 Hint
Common Mistakes
Using HTTP URLs instead of MongoDB connection strings.
Missing quotes around the connection string.
3fill in blank
hard

Fix the error in the Express route that saves data to the database.

Express
app.post('/add', async (req, res) => {
  const item = new Item({ name: req.body.[1] });
  await item.save();
  res.send('Saved');
});
Drag options to blanks, or click blank then click option'
Adata
Btitle
Cname
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using a field name that does not exist in the model.
Forgetting to parse JSON body before accessing it.
4fill in blank
hard

Fill both blanks to create a Mongoose schema and model for a user.

Express
const userSchema = new mongoose.Schema({
  [1]: String,
  [2]: Number
});
const User = mongoose.model('User', userSchema);
Drag options to blanks, or click blank then click option'
Ausername
Bemail
Cage
Dpassword
Attempts:
3 left
💡 Hint
Common Mistakes
Using email as a number type.
Confusing password with age.
5fill in blank
hard

Fill all three blanks to query users older than 18 and sort by username.

Express
User.find({ [1]: { [2]: 18 } })
  .sort({ [3]: 1 })
  .exec((err, users) => {
    if (err) return console.error(err);
    console.log(users);
  });
Drag options to blanks, or click blank then click option'
Aage
B$gt
Cusername
Demail
Attempts:
3 left
💡 Hint
Common Mistakes
Using '$lt' instead of '$gt' for greater than.
Sorting by email instead of username.