const {users, name} = req.body; const user = await User.findById(req.user.id).select('-password'); const newGroup = new Group ({ name: name, users: [], admin: user.id }); if (users) { for (const usr of users) { let person = await User.findOne({username:usr}); if(!person){ return res.json({error: 'User not found'}); } else { newGroup.users.push(person.id); } } } const result = await newGroup.save(); res.json(result); The big issue is that there's validation in the middle of it, and there's no avoiding it. The other issue is that if I put the validation within a service layer, I need to insure that the rest of the code doesn't run, but I can't if I don't return false, and then false again and I handle the false check within the route. So is there something I can do to make the code cleaner and avoid having to do something like:if(!service.validationUsers()) { return false; } orif(!service.createGroup()) { return false; } Also, how I do add a function inside a service layer class? It looks like I am going to have to handle the case where users is a string, and I don't want to copy and paste code.
Submitted November 15, 2019 at 01:44AM by jesusscript
No comments:
Post a Comment