Please Help I am working on the MERN project. In this project we allow the user to create an account and they can follow other users too and all those followed users will be added to their friends. Following is the code which I have used on the back-end to get all friends of a user. Now For the admin panel, I want to get all users from the database because the admin has the right to delete any account. Is there any way I can get all users from the database? Please Feel Free to ask for more details Following is the code that I have used to get all friends of a user
//get friends
router.get("/friends/:userId", async (req, res) => {
try {
const user = await User.findById(req.params.userId);
const friends = await Promise.all(
user.followings.map((friendId) => {
return User.findById(friendId);
})
);
let friendList = [];
friends.map((friend) => {
const { _id, username, profilePicture } = friend;
friendList.push({ _id, username, profilePicture });
});
res.status(200).json(friendList)
} catch (err) {
res.status(500).json(err);
}
});
Here is the complete code of users.js
const User = require("../models/User");
const router = require("express").Router();
const bcrypt = require("bcrypt");
//update user
router.put("/:id", async (req, res) => {
if (req.body.userId === req.params.id || req.body.isAdmin) {
if (req.body.password) {
try {
const salt = await bcrypt.genSalt(10);
req.body.password = await bcrypt.hash(req.body.password, salt);
} catch (err) {
return res.status(500).json(err);
}
}
try {
const user = await User.findByIdAndUpdate(req.params.id, {
$set: req.body,
});
res.status(200).json("Account has been updated");
} catch (err) {
return res.status(500).json(err);
}
} else {
return res.status(403).json("You can update only your account!");
}
});
//delete user
router.delete("/:id", async (req, res) => {
if (req.body.userId === req.params.id || req.body.isAdmin) {
try {
await User.findByIdAndDelete(req.params.id);
res.status(200).json("Account has been deleted");
} catch (err) {
return res.status(500).json(err);
}
} else {
return res.status(403).json("You can delete only your account!");
}
});
router.delete("/admin/:id", async (req, res) => {
if (req.body.user == req.body.isAdmin) {
try {
await User.findByIdAndDelete(req.params.id);
res.status(200).json("Account has been deleted");
} catch (err) {
return res.status(500).json(err);
}
} else {
return res.status(403).json("You can delete only your account!");
}
});
//get a user
router.get("/", async (req, res) => {
const userId = req.query.userId;
const username = req.query.username;
try {
const user = userId
? await User.findById(userId)
: await User.findOne({ username: username });
const { password, updatedAt, ...other } = user._doc;
res.status(200).json(other);
} catch (err) {
res.status(500).json(err);
}
});
//get friends
router.get("/friends/:userId", async (req, res) => {
try {
const user = await User.findById(req.params.userId);
const friends = await Promise.all(
user.followings.map((friendId) => {
return User.findById(friendId);
})
);
let friendList = [];
friends.map((friend) => {
const { _id, username, profilePicture } = friend;
friendList.push({ _id, username, profilePicture });
});
res.status(200).json(friendList)
} catch (err) {
res.status(500).json(err);
}
});
//follow a user
router.put("/:id/follow", async (req, res) => {
if (req.body.userId !== req.params.id) {
try {
const user = await User.findById(req.params.id);
const currentUser = await User.findById(req.body.userId);
if (!user.followers.includes(req.body.userId)) {
await user.updateOne({ $push: { followers: req.body.userId } });
await currentUser.updateOne({ $push: { followings: req.params.id } });
res.status(200).json("user has been followed");
} else {
res.status(403).json("you allready follow this user");
}
} catch (err) {
res.status(500).json(err);
}
} else {
res.status(403).json("you cant follow yourself");
}
});
//unfollow a user
router.put("/:id/unfollow", async (req, res) => {
if (req.body.userId !== req.params.id) {
try {
const user = await User.findById(req.params.id);
const currentUser = await User.findById(req.body.userId);
if (user.followers.includes(req.body.userId)) {
await user.updateOne({ $pull: { followers: req.body.userId } });
await currentUser.updateOne({ $pull: { followings: req.params.id } });
res.status(200).json("user has been unfollowed");
} else {
res.status(403).json("you dont follow this user");
}
} catch (err) {
res.status(500).json(err);
}
} else {
res.status(403).json("you cant unfollow yourself");
}
});
module.exports = router;