'Issue trying to get the data from an xlxs file into mongo db using mongoose and read-excel-file

First of all, I'm sorry, English is not my first language. So I got the code working with a local file to parse it into json. But I can't get the file with a request from the node side. I'm not exactly sure what request or response I should be doing. This is my current POST controller

exports.manyOperadorasPost = async (req, res) => {

let operadoraFile = req.files
console.log(operadoraFile)
await readXlsxFile(operadoraFile).then(function(data){
    let i = 0
    let headers = []
    let json_object = []

    // assign headers to row, so we have a key value pair to conversion
    data.map((row, index) => {
        if(i == 0){
            headers = row;
        }
        // import data
        if(i > 0){
            let temp = {}
            for(let j = 0; j < row.length; j++){
                temp[headers[j]] = row[j]
            }
            json_object.push(temp)
        }
        i++
    })
    let operadoras_new = JSON.stringify(json_object, null, 2)
})
await Operadora.insertMany(operadoras_new)

try{
    res.redirect('/operadoras')
}catch(error){
    res.send(error)
    }
}

I've got this two routes created:

router.get('/upload/manyOperadoras', auxController.manyOperadoras)
router.post('/upload/manyOperadoras', express.urlencoded({extended:true}), auxController.manyOperadorasPost)

I'm using Bootstrap 5 and EJS also. So here's my current EJS file segment for the file:

<form class="d-flex justify-content-between" method="POST" enctype="multipart/form-data" action="/upload/manyOperadoras">  
      <input type="file" class="form-control " id="operadoraFile" name="operadoraFile">
      <button type="submit" class="input-group-text btn btn-info" id="operadoraBtn">+ Operdadoras</button>
   </form>

Please, any help would be much appreciated. Thank you in advance.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source