Monday, 15 May 2017

Wierd bug with PUT request NodeJS (sometime updates properly, sometimesn not)

This is my put request, that gets fired when person is done editing their profile.

//Update
router.put("/:id", middleware.checkOwnership, function(req,res){
   person.findByIdAndUpdate(req.params.id, req.body.person,  function(err, updatedPerson) {
      // Return today's date and time
      var currentTime = new Date();
      // returns the month (from 0 to 11)
      var month = currentTime.getMonth();
      // returns the day of the month (from 1 to 31)
      var day = currentTime.getDate();
      // returns the year (four digits)
      var year = currentTime.getFullYear();
      //Extra
      var extramealholder = req.body.person.extrameal.split(":");
      console.log("Split holder -> " + extramealholder)
      var extramealhour = parseInt(extramealholder[0]);
      var extramealminute = parseInt(extramealholder[1]);
      updatedPerson.extrameal = moment({ year :year, month :month, day :day, hour :extramealhour, minute :extramealminute}).format("YYYY-MM-DD HH:mm");
      // Meal times if fixed schedule1
      if(err) {
         req.flash("error", "ERROR: My fuckin god.. something went wrong there! Contact customer support!");
         res.redirect("/food");
      } else {

      if(req.body.person.usingfixedschedule == "true") {
         var breakfastholder = req.body.person.breakfast.split(":");
         console.log("Split holder -> " + breakfastholder);
         var breakfasthour = parseInt(breakfastholder[0]);
         var breakfastminute = parseInt(breakfastholder[1]);

         var lunchholder = req.body.person.lunch.split(":");
         var lunchhour = parseInt(lunchholder[0]);
         var lunchminute = parseInt(lunchholder[1]);

         var dinnerholder = req.body.person.dinner.split(":");
         var dinnerhour = parseInt(dinnerholder[0]);
         var dinnerminute = parseInt(dinnerholder[1]);

        updatedPerson.breakfast = moment({ year :year, month :month, day :day, hour :breakfasthour, minute :breakfastminute}).format("YYYY-MM-DD HH:mm");
        updatedPerson.lunch = moment({ year :year, month :month, day :day, hour :lunchhour, minute :lunchminute}).format("YYYY-MM-DD HH:mm");
        updatedPerson.dinner = moment({ year :year, month :month, day :day, hour :dinnerhour, minute :dinnerminute}).format("YYYY-MM-DD HH:mm");
      } else {
         if(updatedPerson.oldbreakfast !== undefined) {
            var breakfastplaceholder3 = updatedPerson.oldbreakfast.split(" ");
            var breakfastplaceholder2 = breakfastplaceholder3[1].split(":");
            var breakfasthour2 = parseInt(breakfastplaceholder2[0]);
            var breakfastminute2 = parseInt(breakfastplaceholder2[1]);

            updatedPerson.lunch = moment({ year :year, month :month, day :day, hour :breakfasthour2, minute :breakfastminute2}).add(parseInt(req.body.person.timeafterbreakfastlunch), "h").format("YYYY-MM-DD HH:mm");
            updatedPerson.dinner = moment({ year :year, month :month, day :day, hour :breakfasthour2, minute :breakfastminute2}).add(parseInt(req.body.person.timeafterbreakfastlunch)+parseInt(req.body.person.timeafterlunchdinner), "h").format("YYYY-MM-DD HH:mm");
         }
      }
      updatedPerson.save();
      res.redirect("/food/" + req.params.id);
      }
   });
});

Problem: updatedPerson.extrameal works fine with fixed schedules, but when person is not having person.usingfixedschedule, then it will give wierd bugs.

For example, either extrameal WILL get set, but then lunch and meal from the person will get deleted, or lunch and meal will work but fixedmeal wont be set. HOW is this happening, i don't even see relation between them.

Problem??? NO IDEA.



via The Garrus

No comments:

Post a Comment