DB connection problem in the advanced Node course

Tell us what’s happening:
Describe your issue in detail here.
The database connection test doesn’t pass, even though i used a try...catch block and used console.log() to check if the connection is successful.

try{
    const myDataBase = await client.db('database').collection('users');
    console.log("connected to db");
  } catch(err){
    console.log("error heppen ", err);
  }

The database i’m trying to connect to in the URI is called “myFirstDatabase”, it exists on my Mongo Atlas account, in case that’s important. But also, i tried changing this code:
await client.db('database').collection('users')
to this:
await client.db('myFirstDatabase').collection('users')
But the db connection test still did not pass.
According to the message in the console, the connection does happen. Please lend me a hand. Thank you :hugs:

Your project link(s)

solution: boilerplate-advancednode - Replit

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59

Challenge: Implement the Serialization of a Passport User

Link to the challenge:

It is crashing for me when I run it.

Error: Failed to lookup view "/views/pug/index.pug" in views directory "/home/runner/boilerplate-advancednode/views"

Try changing the res.render code to what is in the code posted in the challenge.

Yeah, i tinkered with it for a while, that’s why you got that error, probably.
Anyway, i removed this piece of code that’s outside and after myDB(async client =>... db connection function:

app.route('/').get((req, res) => {
  res.render(process.cwd() + '/views/pug', {title: 'Hello', message: 'Please login'});
});

At the same time, inside the myDB(async client =>... db connection function,
there’s this piece of code:

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

Which basically gets ignored, cause the one from outside comes after it.
It seems like the tests check the existence of a db connection against this data from the frontend:

{
      title: 'Connected to Database',
      message: 'Please login'
}

which is a bit weird to me, cause the connection was there, but the test was designed to look for these title and message and not the connection itself.
Anyway, that’s what solved it for me.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.