1
I am creating a website in React, Nodejs and Mongodb for a course project and I have a question in my login.
My login prompts email and password to the user, but I created my method on the front that calls the get
based on the id
user because it is the variable that I use in aggregation
with my database to fetch your Roster
work. I put a id
test to verify and works well, but I need to know how to get this value id
of my backend.
It would be necessary to make another aggregate
?
Follow the code on my frontend:
<button onClick={getRosterByUser}>roster</button>
const getRosterByUser = () => {
Axios({
method: 'GET',
withCredentials: true,
url: `http://localhost:4000/roster/5fd778b02e61463ea46eadf9`
}).then((res) => {
setRoster(res.data);
console.log(res.data);
});
};
Code of the backend:
app.get('/roster/:_id', (req, res) => {
const _id = req.params._id;
Roster.aggregate(
[
{
$match: {
userId: objectId(_id)
}
},
{ $unwind: '$week' },
{
$lookup: {
from: 'store',
localField: 'week.store_id',
foreignField: '_id',
as: 'user_roster'
}
},
{
$project: {
week: 1,
roster: {
$arrayElemAt: ['$user_roster', 0]
}
}
}
],
function (err, doc) {
console.log(err);
res.send(doc);
}
);
});
If you want to take the back end id to the front normally if you use the ejs template for this or other methods as well, more like using React recommend seeing some template to exchange information between them.
– user212376