[SOLVED] How to get query result into an array in NodeJs?

Issue

I have a page where I upload files into a server (local), and I save the data (name, root, size) of every file into a SQL db. I have to get an array with this information, in PHP i do this with:

mysqli_fetch_array($result, MYSQLI_ASSOC)

Then i have to return this array to an HTML to see this values in a table, but i can´t get this array (I don´t know how).

This is my code in Node:

router.post('/uploadfile', upload.single('archivo'), async(req, res)=>{

let file_name = req.file.filename;
let file_root = req.file.path;
let file_size = Math.round(req.file.size /1024 /1024) + " Mb";
console.log(file_size);
const results = [];
await pool.query('INSERT INTO archivos (name, root, size) VALUES (?, ?, ?)', [file_name, file_root, file_size], function (err, results, fields){
    //HERE I HAVE TO GET THE ARRAY
});

res.render('index.html', {results: results}); // HERE I HAVE TO PASS THE ARRAY

//console.log(archivo);

})

Solution

I only have to use, after a SELECT query, this:

results[0].name;

This is how i got the name of the first element. Thanks!

Answered By – Gaspar Giménez

Answer Checked By – Timothy Miller (BugsFixing Admin)

Leave a Reply

Your email address will not be published.