1

I have two collections Developer and project. I want to assign more then one project to developer but i don't want to repeatedly assign any project.

/*@Submit assign project to developer */
exports.putDevAssign = (req, res, next) => {
    const {projectId, teamId} = req.body

    Team.findById(teamId)
    .then(result => {
        if(result.project.length == 0 ) {
            Team.findByIdAndUpdate(teamId, {$push: { project : projectId } }, { new: true }).populate('project')
                    .then(result => {
                        res.redirect('/Assigned-Developer/' + teamId)
                    })
                    .catch(err => console.log(err))
        } else {
            for(let i = 0; i < result.project.length; i++) {
                if( projectId == result.project[i] ) {  
                    req.flash('message', 'Already Assigned')
                    return res.redirect('/Assigned-Developer/' + teamId)
                } else {
                    console.log('hello')
                    Team.findByIdAndUpdate(teamId, {$push: { project : projectId } }, { new: true }).populate('project')
                    .then(result => {
                        return res.redirect('/Assigned-Developer/' + teamId)
                    })
                    .catch(err => console.log(err))
                }
            }
        }
    })
    .catch(err => console.log)
}

But in above code i can assign first project and it is neither repeatedly stored. but from second project it is stored repeatedly . I don't want this to happen. i want to assign only unique project. Help me with this silly error that i have made. Thank you in advance.

1 Answer 1

1

When you're comparing the existing projects, you're checking each element and adding the project if that element does not match. You should compare all the current projects before adding the new one.

Try this code:

/*@Submit assign project to developer */
exports.putDevAssign = (req, res, next) => {
    const {projectId, teamId} = req.body

    Team.findById(teamId)
    .then(result => {
        if(result.project.length == 0 ) {
            Team.findByIdAndUpdate(teamId, {$push: { project : projectId } }, { new: true }).populate('project')
                    .then(result => {
                        res.redirect('/Assigned-Developer/' + teamId)
                    })
                    .catch(err => console.log(err))
        } else {
            for(let i = 0; i < result.project.length; i++) {
                if( projectId == result.project[i] ) {  
                    req.flash('message', 'Already Assigned')
                    return res.redirect('/Assigned-Developer/' + teamId)  // project already in list
                }
            }
            // if code reached here, project not in current project list, must add it
            console.log('hello')
            Team.findByIdAndUpdate(teamId, {$push: { project : projectId } }, { new: true }).populate('project')
            .then(result => {
                return res.redirect('/Assigned-Developer/' + teamId)
            })
            .catch(err => console.log(err))
            }
        }
    })
    .catch(err => console.log)
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank You sir! You gave pills for my headache.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.