Serialization of a User Object - purpose of ('users')?

Hi,

Question regarding the block of code below, given by the lesson "Serialization of a User Object".

passport.serializeUser((user, done) => {
  done(null, user._id);
});
passport.deserializeUser((id, done) => {
  db.collection('users').findOne(
    {_id: new ObjectID(id)},
      (err, doc) => {
        done(null, doc);
      }
  );
});

Question:
I can’t figure out the purpose of (‘users’) in passport.deserializeUser.

This to me, is only a string, without referencing to anywhere in the Glitch project given by question.

Could someone please explain to me the purpose of (‘user’)?
For example, what happens if I don’t include (‘user’)?

Thank you,

‘user’ refer to your database collection.
In MongoDB, each database has its own collection where you can name it anything.
In this example, the collection name is 'user'.
So if you don’t include it, your database can’t figure out which collection you target is.
More about collections: https://docs.mongodb.com/manual/core/databases-and-collections/#collections

1 Like

Thank you @padunk

However, I’m still confused about, where was it defined in the Glitch project, of the collection name being ‘user’ ?

Perhaps I’m overlooking some very obvious facts, however if someone could enlighten me, point out where in the project that ‘user’ was defined for db.collection, it’d be appreciated.

Cheers,

‘users’ is collection name and it is present in mongodb database. You are just retriving data from that collection.

I believe there is a note in that challenge;

NOTE: This deserializeUser will throw an error until we set up the DB in the next step so comment out the whole block and just call done(null, null) in the function deserializeUser. Submit your page when you think you’ve got it right.

So, just follow the steps.

Hi,

Again many thanks for your time.

I did so, call done(null, null) and passed the challenge already.

I’m just wondering, in which challenge, was it ever that the we defined the collection ‘users’.

In my understanding, you have to at least define a variable in order to use it. Otherwise, I can’t see how we can access something in the mongodb database, without it being present (defined).

Cheers,

Hi all,

My confusion has been solved. I realized that (‘users’) is a collection name, as many replies to this thread mentioned already, that perhaps will be defined somewhere later in the ''Advanced Node and Express" challenge.

I was originally confused by the fact that, without even having it defined, the challenge went straight into .findOne (a search method). Usually, programs would define the collection first, before conducting .findOne method, perhaps somewhere in later challenge we’ll see ('users) being specifically defined and actually being loaded with user data.
(Otherwise it wouldn’t make sense to conduct database search without defining it)

https://www.tutorialsteacher.com/nodejs/access-mongodb-in-nodejs
I read the above explanation, which is good in introducing basic MongoDB.

Thanks a lot for everyone’s time.