I'm trying to retrieve multiple users from a database, but I'm not sure how to make the query.
I get the IDs from the endpoint like this: localhost:1000/api/user/1,2,3,4
where 1,2,3,4
are the ids of 4 different users.
What I tried to do is to use .split() on the request but I am not able to solve it..
To get all the users I am using this function:
function GetAll(req, res){
userSchema
.find({},{_id:0, __v:0})
.then((data) => res.json(data))
.catch((error) => res.json({ msg: error }))
};
This is to retrieve a single user:
function GetUser(req, res){
const { id } = req.params // '1,2,3,4';
userSchema
.find({id}, {_id:0, __v:0})
.then((data) => res.json(data))
.catch((error) => res.json({msg: error }))
};
I tried to do the split()
in GetUser
, which only looks for a single user... Is there an easier way to solve it?
I tried something like this:
function GetUser(req, res){
const { id } = req.params;
const idSplited = id.split(",");
userSchema
.find({id}, {_id:0, __v:0})
.then((data)=> data = data.filter((item) => idSplited.includes(item.id)) = res.json(data))
.catch((error) => res.json({msg: error }))
};