Reputation: 167
I want to send the values of a form to Express.js to then save them in database. But the only value that is not reaching Express.js is the value in the select element. The form sends two things it sends an excel file and the value inside the select element. I even tried to console.log the req.body to see if the value is sent in the request body but it returns a void {} value. Here is the HTML.
<div class="w3-card-2 w3-display-middle" class="margin:auto">
<header class="w3-container w3-blue"><h4><b>Subir documento de Excel</b></h4></header>
<form id="uploadForm" enctype="multipart/form-data" action="/upload" method="post">
<br>
<input type="file" name="file" />
<br>
<br>
<label>Entidad financiera: </label>
<select name="bancos" class="w3-select w3-border"> /* <---------- */
<option value="noAsignado">No asignado</option>
<option value="bhdLeon">BHD Leon</option>
<option value="asociacionNacional">ASOCIACION NACIONAL DE AHORROS Y PRESTAMOS</option>
<option value="pucmm">PUCMM</option>
<option value="grupoAltus">GRUPO ALTUS</option>
</select>
<br>
<br>
<br>
<br>
<input class="w3-display-bottommiddle" type="submit" value="Subir" name="submit">
</form>
Here the Node code:
app.post('/upload', function(req, res){
console.log(req.body); // <---------------------
var exceltojson;
upload(req, res, function(err){
if (err) {
res.json({error_code:1,err_desc:err});
return;
}
if(!req.file){
res.json({error_code:1, err_desc:"No file passed"});
return;
}
if(req.file.originalname.split('.')[req.file.originalname.split('.').length-1] === 'xlsx'){
exceltojson = xlsxtojson;
} else {
exceltojson = xlstojson;
}
try {
exceltojson({
input: req.file.path,
output: "./outPutJSON/output.json",
lowerCaseHeaders: true
}, function(err, result){
if(err){
return res.json({error_code:1, err_desc:err, data: null});
}
res.json({datos:"Los datos fueron agregados exitosamente"});
fs.readFile("./outPutJSON/output.json", 'utf8', async (err, fileContents) => {
if (err) {
console.error(err);
return;
}
try {
let data = JSON.parse(fileContents);
console.log(data); //--------------HERE THE EXCEL FILE WHEN IS PARSED
io.emit('test event', 'Se han subido ' + data.length + ' casos' );
for(let cantidad = 0; cantidad < data.length; cantidad++){
var documento = data[cantidad];
if(documento.nombre === '' || documento.cedula === '' || documento.direccion === '') {
console.log('No se puede guardar este documento');
} else {
var mostrar = await incrementar();
documento.caso = mostrar;
documento.montoApoderado = data[cantidad]['monto apoderado'];
documento.numeroCliente = data[cantidad]['no.cliente'];
documento.numeroProducto = data[cantidad]['no.producto'];
let today = moment().format('YYYY M D');
documento.fechaCreado = today;
var mod = new model(documento);
await mod.save(documento);
}
}
} catch(err) {
console.error(err);
}
})
});
var fs = require('fs');
try {
fs.unlinkSync(req.file.path)
}catch(e){
}
} catch (e) {
res.json({error_code:1, err_desc:"Corrupted excel file"});
}
});
});
Upvotes: 1
Views: 581
Reputation: 1214
1-) Check that your express.js use the next sentences before calling the router methods:
app.use(bodyParser);
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: false })); // support encoded bodies
2-) You don't have an ID attribute attached to your tag and NAME attributes to select options (although the name is the one necessary for the server-side... :/)
You need to re-write your js code:
<select id="banks" name="bancos" class="w3-select w3-border"> /* <---------- */
<option name="noAsignado" value="noAsignado">No asignado</option>
<option name="bhdLeon" value="bhdLeon">BHD Leon</option>
<option name="asociacionNacional" value="asociacionNacional">ASOCIACION NACIONAL DE AHORROS Y PRESTAMOS</option>
<option name="pucmm" value="pucmm">PUCMM</option>
<option name="grupoAltus" value="grupoAltus">GRUPO ALTUS</option>
</select>
3-) Maybe you are checking the body before going through the next() function. Try before executing the handler and there, check the req.body again. :)
Best regards.
Upvotes: 1