Meteor.js, multiple insertions in different collections by submitting a form

51 Views Asked by At

I am trying to insert elements in 2 different collections with a submit. Here is the problem : When I try the first submit, it only works for the first insert. If I change the order of inserts, it only works for the first insert too. The problem is only present during the first submit after launching the server.

import { Template } from 'meteor/templating';
import './bon-creer.html';

import {Hotels} from "../../api/hotels.js";
import {Beneficiaires} from "../../api/beneficiaires";
import {BonsNuitees} from "../../api/bons_nuitees";


Template.bonCreer.helpers({
    beneficiaires () {
        return Beneficiaires.find({});
    },
    hotels () {
        return Hotels.find({});
    },
    bonsNuitees() {
        return BonsNuitees.find({});
    },
});

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

        event.preventDefault();

        var beneficiaires = [];
        var nbBeneficiaires = 1;

        ////// 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,
            ageB : 0,
            typologieB : typologie,
            nationaliteB : nationalite,
            statutB : statut,
            violencesB: "",
            antecedentsB: ""
        };

        ////// Bon de nuitee(s) //////
        beneficiaires.push(beneficiaire);
        var hotel = $("select[name='hotel']").val();
        var chambres = $("input[name='chambres']").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,
            nomH : hotel,
            chambresH : chambres,
            date_debut : date_debut,
            date_fin : date_fin,
            //prix : prix
        };


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


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

How to fix this problem please?

0

There are 0 best solutions below