Commit 9259f210 authored by Balasuriya D.A.M.'s avatar Balasuriya D.A.M.

Search User API

parent eb3d178e
...@@ -63,4 +63,26 @@ const authUser = asyncHandler(async (req, res) => { ...@@ -63,4 +63,26 @@ const authUser = asyncHandler(async (req, res) => {
} }
}); });
module.exports = { registerUser, authUser }; // /api/user?search = minosh - goint to creat search query
\ No newline at end of file //This is how to access the query (search query)
const allUsers = asyncHandler(async (req, res) => {
const keyword = req.query.search ? {
//use $or operation
$or: [
//references from MongoDB pages.can get more information from that about $regex
{ name: { $regex: req.query.search, $options: "i" } },
{ email: { $regex: req.query.search, $options: "i" } },
],
}
: {};
//query write to database
const users = await (await User.find(keyword)); //.find({ _id: { $ne: req.user._id } });
// find({_id:{$ne:req.user._id}}) - current id user loged in
res.send(users); //to return
});
module.exports = { registerUser, authUser,allUsers };
\ No newline at end of file
const express = require("express"); const express = require("express");
const { registerUser,authUser } = require("../controllers/userControllers"); const { registerUser,authUser,allUsers } = require("../controllers/userControllers");
const router = express.Router(); const router = express.Router();
router.route("/").post(registerUser); router.route("/").post(registerUser).get(allUsers); //User searching API end point
router.post("/login",authUser ); router.post("/login", authUser);
module.exports = router; module.exports = router;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment