Jack Z
Jack Z

Reputation: 53

MongoDB unique users

I am trying to create a login page where users can add favourite food item, but I'm not sure how exactly I would make it so that it knows exactly which user is logged in. I want to ultimately be able to edit add things so each specific user has its own unique list of favourite items. I've tried looking everywhere, but I can't seem to find any information. I am looking for advice on a general direction such as specific packages etc.

Upvotes: 1

Views: 99

Answers (1)

Robert
Robert

Reputation: 322

If I understand your question correctly - you want to create a page that is only accessible by users who are logged in, and you want to use the unique id of that user so that you can determine a list of favourite items for the user?

The way that you can do this is by creating models for the data that you're looking to store - one for the user and one for favorite items. Your models would probably look something like this:

user: {
  firstName: string,
  lastName: string
}

and the favourite items would be

itemsArray: [
  {
    itemName: string
  }
]

Mongoose is the package that you will want to use for modeling. You can read more about it here https://mongoosejs.com/docs/models.html. (the package is https://www.npmjs.com/package/mongoose).

Also note that in MongoDB, all documents are given a unique id automatically. You don't need to include them in the model. So your user would really look more like this in the database, once they've been entered:

user: {
  _id: 507f191e810c19729de860ea,
  firstName: "John",
  lastName: "Doe"
}

Let me know if this helps.

Upvotes: 1

Related Questions