Skip to main content

--description--

You are not loading an actual user object since the database is not set up. Connect to the database once, when you start the server, and keep a persistent connection for the full life-cycle of the app. To do this, add your database's connection string (for example: mongodb+srv://<username>:<password>@cluster0-jvwxi.mongodb.net/?retryWrites=true&w=majority) to the environment variable MONGO_URI. This is used in the connection.js file.

If you are having issues setting up a free database on MongoDB Atlas, check out this tutorial.

Now you want to connect to your database, then start listening for requests. The purpose of this is to not allow requests before your database is connected or if there is a database error. To accomplish this, encompass your serialization and app routes in the following code:

myDB(async client => {
const myDataBase = await client.db('database').collection('users');

// Be sure to change the title
app.route('/').get((req, res) => {
// Change the response to render the Pug template
res.render('index', {
title: 'Connected to Database',
message: 'Please login'
});
});

// Serialization and deserialization here...

// Be sure to add this...
}).catch(e => {
app.route('/').get((req, res) => {
res.render('index', { title: e, message: 'Unable to connect to database' });
});
});
// app.listen out here...

Be sure to uncomment the myDataBase code in deserializeUser, and edit your done(null, null) to include the doc.

Submit your page when you think you've got it right. If you're running into errors, you can check out the project completed up to this point.

--hints--

Database connection should be present.

async (getUserInput) => {
const url = new URL("/", getUserInput("url"));
const res = await fetch(url);
const data = await res.text();
assert.match(
data,
/Connected to Database/gi,
'You successfully connected to the database!'
);
}

Deserialization should now be correctly using the DB and done(null, null) should be called with the doc.

async (getUserInput) => {
const url = new URL("/_api/server.js", getUserInput("url"));
const res = await fetch(url);
const data = await res.text();
assert.match(
data,
/null,\s*doc/gi,
'The callback in deserializeUser of (null, null) should be altered to (null, doc)'
);
}