Как сделать массив с выводом формы и опубликовать с выражением в mongodb - PullRequest
0 голосов
/ 14 октября 2019

У меня есть эта схема mongoose

const AsientoSchema = new Schema({
    numero_asiento: Number,
    fecha: Date,
    cuentas: {type:Array,"default":[]},

    // name: { type: Schema.ObjectId, ref: "Cuenta" }

    // type: String,
    // type: [{ type: Schema.Types.ObjectId, ref: 'counts' }]
})

И у меня есть две формы HTML5, первая из них состоит в том, чтобы поместить эти значения в схему mongoose "asiento", а вторая форма создает "cuenta"

<h1>crear asiento</h1>
<form action="/crear-asiento" method="post" class="asientoForm">
        <strong>Fecha: </strong>         
        <input type="date" name="fecha" required>
        <br>
        <br>      
        <button type="submit" class="asientoButton">Añadir asiento</button>
</form>
<form action="/crear-asiento" method="post" class="cuentasForm">
        <strong>Elija cuenta: </strong> 
        <select name="nombrecuenta" id="">
                <% for(var i=0; i < asientos.length; i++) { %>
                <option value="<%= asientos[i].name %>"><%= asientos[i].name %></option>
                <% } %>
        </select>
        <input type="radio" name="dh" value="debe" checked> Debe
        <input type="radio" name="dh" value="haber"> Haber
        <br>
        <br>
        <strong>Importe: </strong>
        <input type="number" name="importe">
        <button type="submit" class="cuentaButton" >Añadir cuenta</button>

</form>

У меня есть это в экспрессе

const express = require('express');
const router = new express.Router(); 
const Asiento = require('../model/asientos'); 
const Count = require('../model/counts')




router.get('/crear-asiento', async (req, res) => {
    const asientos = await Count.find();//.populate("name");
    const nvo_asiento = await Asiento.find();//.populate("name");
    res.render('crear-asiento', {    
        asientos, nvo_asiento  
    });

});

router.post('/crear-asiento', async (req, res, next) => {
    const nvo_asiento = new Asiento(req.body);


    await nvo_asiento.save(); //para que se almacene en la db con async await 
    res.redirect('/crear-asiento');  // redirecciona a la ruta inicial del servidor
});

module.exports = router

Итак, я хочу создать несколько объектов "cuenta" с формой и вставить в массив "cuentas". Как я могу это сделать? Я пытался сохранить их в массив с функциями js в файле ejs или в экспрессе, но ничего не получалось ..

Я начинаю кодировать с помощью Express и Mongodb, может быть, я не понимаю, что-то базовое

...