Reputation: 75
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}
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.
The loop that Dori Lahav Waisberg suggested seem to format my data, but I cannot access the fields with the dot operator.
Upvotes: 0
Views: 53
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