lundi 17 décembre 2018

Dealing with checkboxes with jade and mongoose

I am struggling with this simple checkbox in form. I want to update the vehicle value (as an array of numbers) in the user schema when this form is submitted. When I put the variable as a value of an invisible input document.getElementById("vals").value = vals; the array is stored as one block of string in the database and when I call [vals] directly in node then it's updated to nothing. I would really appreciate it if you tell me what I am doing wrong.

my jade code:

form.form(action='/volDetails', method='POST')
  .form-group
    input(type='checkbox', value='1', name= 'vehicle[type]')
    label(for='vehicle[type]')
    | Car
    input(type='checkbox', value='2', name= 'vehicle[type]')
    label(for='vehicle[type]')
    | Van 
    input(type='checkbox', value='3', name= 'vehicle[type]')
    label(for='vehicle[type]')
    | Bike 
    input(type='checkbox', value='4', name= 'vehicle[type]')
    label(for='vehicle[type]')
    | bus
    input(type='checkbox', value='5', name= 'vehicle[type]')
    label(for='vehicle[type]')
    | disabled 
script.
   var vals = [];
   $(document).ready(function () {
       var $checkes = $('input:checkbox[name="vehicle[type]"]').change(function () {
           vals = $checkes.filter(':checked').map(function () {
               return parseInt(this.value);
           }).toArray();
       });
   }); 

my node code:

app.post('/volDetails', function(req, res) {
  var conditions = mongoose.model('User').findOne({
            userId: req.user.userId
        }, function(err, doc) {
            doc.vehicle = req.body.vals;
            doc.save(function(err) {
                res.redirect('/');
            });
        }
    );
})




Aucun commentaire:

Enregistrer un commentaire