title: NodeJS MongoDB Callback Hell
body: <p>I'm still having trouble with conditionals and database callbacks, I wrote a routing function that querys for a violation object and this is from a form filled out in the client side. I am trying to change the user that owns this violation as an option, I query for that user and return it fine however at the if statement, x != y will always return true because of the nature of how I wrote this. I want it to finish querying, and then hit the conditional. I feel kind of stuck on how to go about this.</p>
  
  <pre><code> if(viol.user != user[0]._id)
  </code></pre>
  
  <p>That way when I edit the user who owns that violation or object, the user doesn't add the same object to itself over and over again.</p>
  
  <pre><code>app.post('/singleViolation/:violation', isAdmin, function(req, res) {
       var violation = req.params.violation;
  
  
       Violation.findById(violation, function(err, viol){
           if (err) throw err;
           if (req.body.cause === '') {
                  req.body.cause = viol.cause;
              }
              if (req.body.date === '') {
                  req.body.date = viol.date;
              }
              if (req.body.location === '') {
                  req.body.location = viol.location;
              }
  
  
              if(req.body.email === '') {
                  req.body.email == viol.user;
              }
  
              if( req.body.email != '') {
  
                  User.find({"local.email" : req.body.email}, function (err, user) {
  
  
                      if (err) {
                          req.body.email = viol.user;
                      }
  
                      if(viol.user != user[0]._id) {
                          typeof viol.user;
                          typeof user[0].id;
                          console.log(viol.user);
                          console.log(user[0]._id);
                          console.log(viol.user === user[0].id);
                          viol.user = user[0];
                          viol.save();
  
                          user[0].local.violations.push(viol);
                          Vehicles.findById(viol.vehicle, function(err, veh) {
  
                              user[0].local.vehicles.push(veh);
                              veh.owner = user[0];
                              veh.save();
                              user[0].save();
  
                          });
  
                      }
                  });
              }
  
              viol.cause = req.body.cause;
              viol.date = req.body.date;
              viol.location = req.body.location;
  
              viol.save();
  
       });
       res.json(req.body);
   });
  app.get('/editViolations/:violations', isAdmin, function(req, res) {
      res.render('editViolations.ejs');
  });
  </code></pre>