When fetching data from MongoDB, the response comes wrapped in an array after it goes through the User.find() function.
For example, one response looks like this:
[{"_id":"62fe3c888e2776ef3c1a010f","username":"Drago D Trial","password":"U2FsdGVkX1867hs26KL0KitTGhWnP9tdVX6AcmI5pWE=","fullname":"Drago DaTrial","firstname":"","surname":"","email":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="97f3e5f6f0f8d7fff8e3faf6fefbb9f4f8fa">[email protected]</a>","position":"QA Tester","userImage":"","locationCity":"","country":"","role":"","company":"","emailAuthorised":true,"professionalBio":"","positionRecentTitle":"","positionRecentCompany":"","companyAuthorised":"","isAdmin":false,"createdAt":"2022-08-18T13:20:08.045Z","updatedAt":"2022-08-18T13:21:02.619Z","__v":0}]
To access and extract just the _id field from this array, you can use the following API route:
router.get('/inviteToJoinTeam/:token/:email', async (req, res) => {
try {
//verify the token against DB
const userToken = req.params.token;
const indivEmailAdd = req.params.email;
try {
const userDetails = await User.find({ email: indivEmailAdd });
const indivIDAdd = userDetails.map(user => user._id);
res.send(indivIDAdd);
} catch(err){
console.log('Failed to retrieve ID');
}
} catch (error) {
res.send('This request failed');
}
});
This code snippet demonstrates how to access and extract the _id field from the database query result.