suigetsuh17
suigetsuh17

Reputation: 75

How to display field contents nicely after retrieving all documents from collection

I have a users collection in MongoDB Cloud, that I'd like to display in my chat app in browser. I've retrieved the names and their privileges with this function below, but the data gets displayed in json format.

outer.get("/chat", function (req, res, next) {
  User.find({}, { fullname: "!null", privilege: "!null" }).exec(function (
    error,
    user,
  ) {
    if (error) {
      return next(error);
    } else {
      if (user === null) {
        const err = new Error("Not authorized! Go back!");
        err.status = 400;
        return next(err);
      } else {
        res.render("chat.pug", { user });
        console.log(user);
      }
    }
  });
});

I just want the name of the user, his privilege below, without the _id field and also formated nicely.

I'm also using pug to render the template, and this code #{user} displays the documentd retreived, I also used User.findById(req.session.userId) to just get his name with #{user.fullname}

enter image description here

I would appreciate any help as this is my diploma thesis!

Edit:

This is the query I'm using to retrieve all documents' fullname and privilege, but I'm not sure it's the best practice.

enter image description here The loop that Dori Lahav Waisberg suggested seem to format my data, but I cannot access the fields with the dot operator.

enter image description here

Upvotes: 0

Views: 53

Answers (1)

Dori Lahav Waisberg
Dori Lahav Waisberg

Reputation: 970

You would need to use a loop in your jade file to display the fields however you like.

Something like:

each user in users
  li
    h1 = user.username
    h2 = user.privilege

But format it however you like.

Upvotes: 1

Related Questions