Double insertion does not work

Hello,
I’m currently trying to create vouchers using a form. When creating a voucher, a beneficiary is added to the “beneficiaires” collection (var BonsNuitees) and a voucher to the “bonsNuitees” collection (var Beneficiaires). However, I encounter a problem when I first create a voucher : the beneficiary is not added to the collection. If a voucher has already been created, the beneficiaries are added to the collection. Here is my code :

Template.bonCreer.events({
    'submit .new-bon'(event) {

        event.preventDefault();

        var beneficiaires = [];

        ////// Bénéficiaires //////
        var nom = $("input[name='nom']").val();
        var prenom = $("input[name='prenom']").val();
        var date_naissance = $("input[name='date_naissance']").val();
        var typologie = $("select[name='typologie']").val();
        var nationalite = $("select[name='nationalite']").val();
        var statut = $("select[name='statut']").val();

        var beneficiaire = {
            nomB : nom,
            prenomB : prenom,
            date_naissanceB : date_naissance,
            typologieB : typologie,
            nationaliteB : nationalite,
            statutB : statut,
            violencesB: "",
            antecedentsB: ""
        };


        var tranchAge = "";
        var dN = new Date(date_naissance);
        var age = Math.floor(((new Date()-dN)/ 1000 / (60 * 60 * 24) ) / 365.25);
        if (age<0){
            tranchAge = "error";
        }
        else if (age<18 && age>=0){
            tranchAge = "-18";
        }
        else if (age>=18 && age<=25){
            tranchAge = "18-25";
        }
        else if (age>=26 && age<=35){
            tranchAge = "26-35";
        }
        else if (age>=36 && age<=50){
            tranchAge = "36-50";
        }
        else {
            tranchAge = "50+";
        }

        var beneficiaireB = {
            nomB : nom,
            prenomB : prenom,
            date_naissanceB : date_naissance,
            ageB: age,
            tranchAge: tranchAge,
            typologieB : typologie,
            nationaliteB : nationalite,
            statutB : statut,
            violencesB: "",
            antecedentsB: ""
        };


        ////// Bon de nuitee(s) //////
        beneficiaires.push(beneficiaireB);
        var nbBeneficiaires = beneficiaires.length;
        var hotel = $("select[name='hotel']").val();
        var chambresSimples = $("input[name='chambresSpl']").val();
        var chambresDoubles = $("input[name='chambresDbl']").val();
        var date_debut = $("input[name='date_debut']").val();
        var date_fin = $("input[name='date_fin']").val();
        //var prix = $("input[name='prix']").val();

        var bon = {
            beneficiaires : beneficiaires,
            nbBeneficiaires : nbBeneficiaires,
            nomH : hotel,
            chambresSimples : chambresSimples,
            chambresDoubles : chambresDoubles,
            date_debut : date_debut,
            date_fin : date_fin,
            //prix : prix
        };

        BonsNuitees.insert(bon);
        Beneficiaires.insert(beneficiaire);

        window.location.href="./bons-nuitees";
    },
});

Could you help me please ?

this is called on the client? If yes, this is usually a “don’t do”. Your issue might be related to security allowance. Your answer might be here: Meteor Methods vs Client-Side Operations - Discover Meteor

2 Likes

I do not understand why it does not work for the first form submission. Autopublish and insecure packages are activated and I also tried putting the insertion permissions to “true”.