JS Function:
function findUsersWithoutProject(req, res, next) {
try {
let benchUser = [];
const project = await Project.find({})
const users = await User.find({}, {
email: 1,
_id: 0
})
for (let i = 0; i < users.length; i++) {
for (let j = 0; j < project.length; j++) {
for (let k = 0; k < project[j].seats.length; k++) {
if (users[i].email !== project[j].seats[k].employee && project[j].seats[k].employee === undefined) {
benchUser.push(users[i].email)
}
}
}
}
const uniq = [...new Set(benchUser)];
return res.json({
users: uniq
})
} catch (error) {
next(error)
}
}
User Emails:
[{
email: 'example1@example.com',
}, {
email: 'example2@example.com',
}, {
email: 'example3@example.com'
}]
Projects Information:
{ _id: 5cc2dd2eb3eea7004c9a7240,
name: 'Project One',
description: 'Lorem Ipsum',
start: '2018-06-01T09:45:00.000Z',
end: '2019-12-31T09:45:00.000Z',
seats:
[
{
skills: [Array],
_id: 5cc2e3cab3eea7004c9a724a,
start: '2018-06-01T09:45:00.000Z',
end: '2019-12-31T09:45:00.000Z',
potentialExtension: '2020-06-31T09:45:00.000Z',
role: 'Dev',
approved: true,
workload: 10,
employee: 'employee1@example.com'
},
{
skills: [Array],
_id: 5cc2e3cab3eea7004c9a7241,
start: '2018-06-01T09:45:00.000Z',
end: '2019-12-31T09:45:00.000Z',
potentialExtension: '2020-06-31T09:45:00.000Z',
role: 'PM',
approved: true,
workload: 20,
employee: 'employee2@example.com'
}
]
}
Objective:
In this scenario, the goal is to retrieve all users who are not assigned to any project seat.
The function iterates through each user and project's seats to check for a match in the email field.
The returned list of users without projects includes 'employee1@example.com' but erroneously also lists 'employee2@example.com'.