2

I have schema like this and subdocument array in the schema

const mongoose = require('mongoose');

const pengirimanTabungChild = new mongoose.Schema({
    tujuan : {
        type : String,
        required : true
    },
    jumlahTabung : {
        type : String,
        required : true
    }
});

const retesterSchemaHarian = new mongoose.Schema({
    namaUnit : {
        type : String,
        required : true,
        trim : true,
        lowercase : true
    },
    banyakTrip : {
        type : String,
        required : true,
        trim : true
    },
    pengirimanTabung : [pengirimanTabungChild],
}, {
    timestamps : true
});

I make a post request like this to post the data

router.post('/retester/harian', auth, async (req, res) => {

    try {
        const retester = new RetesterHarianData({
            ...req.body,
            namaUnit : req.user.unit,
        });


        await retester.save();
        res.status(201).send(retester);
    } catch (e) {
        console.log("error " + e);
        res.status(400).send(e);
    }
});

but I'm confused how to add data using web client in form and the array size in subdocument added dynamically .. how to get the req.body from the form

the form might look like this

<form action="/retester/harian" method="POST">
    <div class="mt-2">
        <label class="block text-sm text-gray-600" for="banyakTrip">Banyak Trip</label>
        <input class="w-full px-5  py-4 text-gray-700 bg-gray-200 rounded" id="banyakTrip" name="banyakTrip" type="Number" required="" placeholder="banyakTrip" aria-label="banyakTrip">
    </div>

    <!-- THIS WILL BE GENERATED AUTOMATICALLY IN CLIENT BASE ON VALUE IN TRIP . -->
    <div class="mt-6">
        <label class="block text-lg text-gray-800 font-black" for="pengirimanTabung">Pengiriman Tabung Trip ${i+1}</label>
        <div class="-mx-3 md:flex mb-6">
            <div class="md:w-1/2 px-3 mb-6 md:mb-0">
                <label class="block text-sm text-gray-600" for="tujuan">Tujuan</label>
                <input class="w-full px-5  py-4 text-gray-700 bg-gray-200 rounded" id="tujuan" name="tujuan" type="text" required="" placeholder="tujuan" aria-label="tujuan">
            </div>
            <div class="md:w-1/2 px-3">
                <label class="block text-sm text-gray-600" for="jumlahTabung">Banyak Tabung</label>
                <input class="w-full px-5  py-4 text-gray-700 bg-gray-200 rounded" id="jumlahTabung" name="jumlahTabung" type="Number" required="" placeholder="Banyak Tabung" aria-label="jumlahTabung">
            </div>
        </div>
    </div>
    <!-- THIS WILL BE GENERATED AUTOMATICALLY IN CLIENT BASE ON VALUE IN TRIP . -->
    <!-- FOR EXAMPLE IF TRIP VALUE IS 2 THE FORM WILL 2 -->
    <!-- GENERATED AGAIN -->
    <div class="mt-6">
        <label class="block text-lg text-gray-800 font-black" for="pengirimanTabung">Pengiriman Tabung Trip ${i+1}</label>
        <div class="-mx-3 md:flex mb-6">
            <div class="md:w-1/2 px-3 mb-6 md:mb-0">
                <label class="block text-sm text-gray-600" for="tujuan">Tujuan</label>
                <input class="w-full px-5  py-4 text-gray-700 bg-gray-200 rounded" id="tujuan" name="tujuan" type="text" required="" placeholder="tujuan" aria-label="tujuan">
            </div>
            <div class="md:w-1/2 px-3">
                <label class="block text-sm text-gray-600" for="jumlahTabung">Banyak Tabung</label>
                <input class="w-full px-5  py-4 text-gray-700 bg-gray-200 rounded" id="jumlahTabung" name="jumlahTabung" type="Number" required="" placeholder="Banyak Tabung" aria-label="jumlahTabung">
            </div>
        </div>
    </div>
</form>

how the data send to sub-document for jumlahtabung and tujuan.

Bryan Lumbantobing
  • 604
  • 1
  • 7
  • 22

0 Answers0