mardi 12 janvier 2016

NodeJs + Express + Sqlite: how to code a clear Select statement which returns a list of data (not the example of the nodejs-sqlite documentation)

Im working in a expressjs + sqlite small webapp for local use. But my problem is when I want to make a full select over a table. All my scripts are in JScript in 'use-strict' mode.

I want to make one SELECT statement and collect data in a json array named listadata. But the problem is that: until I don't declare this variable outside of the module.exports group -i.e. as a global variable- I cannot return any list of rows. If I delete that declaration, my function doesnt return anything.

This module must return a full list of rows. Please can anyone explain why I must declare this variable as global?. Or maybe is there a way to deal with this kind of statement in a more elegant way according to Javascript?

var listadata = [];
 module.exports = {
listas2 : function(){
    var db = sqlite.connect();
    db.all("SELECT * FROM TBWOHPARG", function(err,rows){
        listadata = [];
        rows.forEach(function(row){
            listadata.push({
                _id: row._id,
                descripcion: row.descripcion,
                fechacreacion: row.fechacreacion,
                fechamodificacion: row.fechamodificacion,
                referencia: row.referencia
            });
        });
    });
    return listadata;
}

Aucun commentaire:

Enregistrer un commentaire