How to get values of selected options by formidable.IncomingForm() in JS?

Viewed 287

I need to get all values of selected options to send db. This is how I get values from Inputs type(text):

let form = new formidable.IncomingForm();
form.parse(req, (err, fields, files) => {
let info = {"_id": Number.parseInt(fields['_id']),
            "name": fields['name'],
            "surname": fields['surname'],
            "phone": fields['phone']}
await dbProcess('visitor','POST',info);
res.writeHead(301, { 'Location': '/visitors' });
res.end();
});

And this is my vash file:

<form id="myform" method="post" action="/visitors" enctype="multipart/form-data" >
                    <div class="form-group">
                        <input type="hidden" class="form-control" name="_id" id="_id">
                    </div>
                    <div class="form-group">
                        <input type="text" class="form-control" name="name" placeholder="Name" id="name">
                    </div>
                    <div class="form-group">
                        <input type="text" class="form-control" name="surname" placeholder="Surname" id="surname">
                    </div>
                    <div class="form-group">
                        <input type="text" class="form-control" name="phone" placeholder="Phone" id="phone">
                    </div>
                    <div class="custom-file">
                        <input type="file" class="custom-file-input" name="photoName" id="customFile" accept=".jpg,.png,.bmp,.jpeg">
                        <label class="custom-file-label" for="customFile" id="photoFile">Choose photo</label>
                    </div>
                    <select multiple>
                    @model.gates.forEach(gate => {
                        <option name="gateId" value="@gate._id">@gate.name</option>
                    });
                    </select>

                
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                        <input type="submit" class="btn btn-primary" value="Save">
                    </div>
                </form>

All the inputs fill by the button click. I can clearly get all values of inputs by applying fields[elementName] or files[elementName] . Now I need to get values of selected options.

0 Answers
Related