Ahmed Basha
Ahmed Basha

Reputation: 51

Data Saved Twice in MongoDB

I have a form to add newCompany, I send data using AJAX like so: Ajax function

And here is the post route: POST route in the back-end

And here is the company model: enter image description here

The problem is, when I add new company the data inserted twice, the first one is 12 fields without the image name!! and the second one is empty object! like so: enter image description here

I have no idea and I gonna lose my mind! Help please!

Upvotes: 0

Views: 1751

Answers (3)

Abdallah
Abdallah

Reputation: 39

I had the same problem as you and i reached this post following your link posted at Udemy.com Q&A board on lecture 37 of the course we're following.

the solution for this problem handle the 'submit' event NOT click event for the button, it looks like the code get executed in response to our wish to handle click event, and then it does execute the natural behavior (and execute the route handler again)

here is the complete validate.js file, just replace the 'click' with 'submit' and this should solve the problem:

$(document).ready(function(){

   $('#register').on('submit',function(e){

       var name     = $.trim($('#name').val());
       var address  = $.trim($('#address').val());
       var city     = $.trim($('#city').val());
       var country  = $.trim($('#country').val());
       var sector   = $.trim($('#sector').val());
       var website  = $.trim($('#website').val());
       var img      = $.trim($('#upload-input').val());

       var isValid = true;

       if(name == ''){

           isValid = false;
           $('#errorMsg1').html('<div class="alert alert-danger">Company name can\'t be empty</div>');
       }

       if(address == ''){

           isValid = false;
           $('#errorMsg2').html('<div class="alert alert-danger">address can\'t be empty</div>');
       }

       if(city == ''){

           isValid = false;
           $('#errorMsg3').html('<div class="alert alert-danger">City can\t be empty</div>');
       }

       if(country == ''){

           isValid = false;
           $('#errorMsg4').html('<div class="alert alert-danger">Country can\'t be empty</div>');
       }

       if(sector == ''){

           isValid = false;
           $('#errorMsg5').html('<div class="alert alert-danger">Sector can\'t be empty</div>');
       }

       if(country == ''){

           isValid = false;
           $('#errorMsg6').html('<div class="alert alert-danger">Website can\'t be empty</div>');
       }

       if(isValid == false){

           return false;

       }else{

           var newCompany = {
               name:        name,
               address:     address,
               city:        city,
               country:     country,
               sector:      sector,
               website:     website,
               img:         img
           };

           $.ajax({
               url:         '/company/create',
               type:        'POST',
               data:        newCompany,
               success:     function(){

                   $('#name').val('');
                   $('#address').val('');
                   $('#city').val('');
                   $('#country').val('');
                   $('#sector').val('');
                   $('#website').val('');
                   $('#upload-input').val('');
               }
           });

       }

   }) 
}); 

Upvotes: 0

Sebastian S
Sebastian S

Reputation: 867

Look this is because you are redirecting your response to company/create

router.post('/company/create', function(req, res, next){

  var newCompany = new Company({
    name: req.body.name,
    address: req.body.address,
    city: req.body.city,
    country: req.body.country,
    .
    .
    .
    .
  });

    Company.save(newCompany, function(err, response){
      if(err){
        console.log(err);
      }

      res.location('/home');
      res.redirect('/home');
    });
});

If it doesn't work, let us see your model. the problem may be there

Upvotes: 0

Dan O
Dan O

Reputation: 6090

you call res.redirect('/company/create') upon save, which sends HTTP302 to the client. The client then triggers the same route a second time. Try performing the redirect on the client side, after your $.ajax() request completes.

Upvotes: 1

Related Questions