Advanced Node and Express Set up the Environment Help

i’ve passed all the test but got an error when i try to login with github

TypeError: Cannot read property 'value' of null at db.collection.findAndModify

on this line: return cb(null, doc.value);

here is the link of challange and the link of my glitch project

my code at auth.js is:

const session     = require('express-session');
const mongo       = require('mongodb').MongoClient;
const passport    = require('passport');
const GitHubStrategy = require('passport-github').Strategy;

module.exports = function (app, db) {
  
    app.use(passport.initialize());
    app.use(passport.session());

    passport.serializeUser((user, done) => {
      done(null, user.id);
    });

    passport.deserializeUser((id, done) => {
        db.collection('chatusers').findOne(
            {id: id},
            (err, doc) => {
                done(null, doc);
            }
        );
    });

    passport.use(new GitHubStrategy({
        clientID: process.env.GITHUB_CLIENT_ID,
        clientSecret: process.env.GITHUB_CLIENT_SECRET,
        callbackURL: "https://environment-setup-uaaeu.glitch.me/auth/github/callback"
      },
      function(accessToken, refreshToken, profile, cb) {
          db.collection('chatusers').findAndModify(
              {id: profile.id},
              {},
              {$setOnInsert:{
                  id: profile.id,
                  name: profile.displayName || 'Anonymous',
                  photo: profile.photos[0].value || '',
                  email: profile.emails[0].value || 'No public email',
                  created_on: new Date(),
                  provider: profile.provider || '',
                  chat_messages: 0
              },$set:{
                  last_login: new Date()
              },$inc:{
                  login_count: 1
              }},
              {upsert:true, new: true}, //Insert object if not found, Return new object after modify
              (err, doc) => {
                  return cb(null, doc.value);
              }
          );
        }
    ));
  
}

full error log is:

/rbd/pnpm-volume/5d8704fe-2e36-48a8-9d35-45bc04172648/node_modules/mongodb/lib/utils.js:123
    process.nextTick(function() { throw err; });
                                  ^
TypeError: Cannot read property 'value' of null
at db.collection.findAndModify (/app/app/auth.js:49:39)
    at handleCallback (/rbd/pnpm-volume/5d8704fe-2e36-48a8-9d35-45bc04172648/node_modules/mongodb/lib/utils.js:120:56)
    at /rbd/pnpm-volume/5d8704fe-2e36-48a8-9d35-45bc04172648/node_modules/mongodb/lib/collection.js:2492:22
    at handleCallback (/rbd/pnpm-volume/5d8704fe-2e36-48a8-9d35-45bc04172648/node_modules/mongodb/lib/utils.js:120:56)
    at /rbd/pnpm-volume/5d8704fe-2e36-48a8-9d35-45bc04172648/node_modules/mongodb/lib/db.js:314:20
    at /rbd/pnpm-volume/5d8704fe-2e36-48a8-9d35-45bc04172648/node_modules/mongodb-core/lib/connection/pool.js:469:18
    at process._tickCallback (internal/process/next_tick.js:61:11)

Were you able to fix it? I’m on the same boat lol

yes i fixed it 5 mins ago.

// changed callback (err, db) to (err, client) to define db = client.db()
mongo.connect(process.env.DATABASE, { useUnifiedTopology: true }, (err, client) => {
  if (err) {
    console.log("Database error: " + err);
  } else {
    console.log("Successful database connection");
    // define db variable to your db name ( you can see the mongodb setup on the picture below)
    let db = client.db('advancednode')
    app.use(
      session({
        secret: process.env.SESSION_SECRET,
        resave: true,
        saveUninitialized: true
      })
    );
    app.use(passport.initialize());
    app.use(passport.session());

    function ensureAuthenticated(req, res, next) {
      if (req.isAuthenticated()) {
        return next();
      }
      res.redirect("/");
    }

    passport.serializeUser((user, done) => {
      done(null, user.id);
    });

    passport.deserializeUser((id, done) => {
      db.collection("socialusers").findOne({ id: id }, (err, doc) => {
        done(null, doc);
      });
    });

    /*
     *  ADD YOUR CODE BELOW
     */

    passport.use(new GitHubStrategy({
      clientID: process.env.GITHUB_CLIENT_ID,
      clientSecret: process.env.GITHUB_CLIENT_SECRET,
      callbackURL: "https://social-authentication-uaaeu.glitch.me/auth/github/callback"
    },
      function(accessToken, refreshToken, profile, cb) {
      console.log(profile);
      //Database logic here with callback containing our user object
        db.collection('socialusers').findAndModify({id: profile.id}, {}, {$setOnInsert:{
          id: profile.id,
          name: profile.displayName || 'uaaeu',
          photo: profile.photos[0].value || '',
          email: profile.emails[0].value || 'No public email',
          created_on: new Date(),
          provider: profile.provider || ''
        },$set:{
          Last_Login: new Date()
        },$inc:{
          Login_count: 1
        }},{upsert: true, new: true}, (err, doc)=>{
          return cb(null, doc.value);
        });
      }
   ));
    
    app.route("/auth/github").get(passport.authenticate("github"));

    app.route("/auth/github/callback").get(passport.authenticate("github", { failureRedirect: "/" }),(req, res) => {res.redirect("/profile");});

    /*
     *  ADD YOUR CODE ABOVE
     */

    app.route("/").get((req, res) => {
      res.render(process.cwd() + "/views/pug/index");
    });

    app.route("/profile").get(ensureAuthenticated, (req, res) => {
      res.render(process.cwd() + "/views/pug/profile", { user: req.user });
    });

    app.route("/logout").get((req, res) => {
      req.logout();
      res.redirect("/");
    });

    app.use((req, res, next) => {
      res
        .status(404)
        .type("text")
        .send("Not Found");
    });

    app.listen(process.env.PORT || 3000, () => {
      console.log("Listening on port " + process.env.PORT);
    });
  }
});

Annotation 2020-08-08 135746