最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Retrieve data from sqlite query Node.js - Stack Overflow

programmeradmin2浏览0评论

I'm currently trying to retrieve data from a sqlite query in node.js, the sql function is on a diferent file so i'm exporting it as a module and then call the function from the index.js. But when i try to retrieve the data the function returns a null value.

Here is my code

Index.js

var express = require("express");
var body_parser = require("body-parser");
var app = express();
var db = require('./dbhandler.js');

app.set("view engine", "jade");

app.get("/data",function(req,res){
   let data = db.select();
   res.send(data);
});

app.get("/",function(req,res){
   res.render("index");
});

app.listen(8888);

dbhandler.js

var sqlite3 = require("sqlite3");

const file = "hr";

exports.select = function (){

var lista = [];

var db = new sqlite3.Database(file);

db.all("SELECT * FROM usuarios", function(err,rows){

    let contador = 0;

    rows.forEach(function (row) {
            lista[contador] = row.nombre + ";" + row.cedula + ";" + row.edad + ";" + row.pais;
    });
});

db.close();

return lista;
}

I'm currently trying to retrieve data from a sqlite query in node.js, the sql function is on a diferent file so i'm exporting it as a module and then call the function from the index.js. But when i try to retrieve the data the function returns a null value.

Here is my code

Index.js

var express = require("express");
var body_parser = require("body-parser");
var app = express();
var db = require('./dbhandler.js');

app.set("view engine", "jade");

app.get("/data",function(req,res){
   let data = db.select();
   res.send(data);
});

app.get("/",function(req,res){
   res.render("index");
});

app.listen(8888);

dbhandler.js

var sqlite3 = require("sqlite3");

const file = "hr";

exports.select = function (){

var lista = [];

var db = new sqlite3.Database(file);

db.all("SELECT * FROM usuarios", function(err,rows){

    let contador = 0;

    rows.forEach(function (row) {
            lista[contador] = row.nombre + ";" + row.cedula + ";" + row.edad + ";" + row.pais;
    });
});

db.close();

return lista;
}
Share Improve this question asked Jun 12, 2016 at 15:52 A.rodriguezA.rodriguez 2701 gold badge4 silver badges11 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 11

Node is asynchronous!!!. lista is returned from the module before the db.all function pletes.

You either need to pass a callback into the select function or return a promise. The callback approach would look something like this:

 exports.select = function (cb){
    var lista = [];
    var db = new sqlite3.Database(file);           
    db.all("SELECT * FROM usuarios", function(err,rows){
         if(err) return cb(err);
         let contador = 0; 
         rows.forEach(function (row) { 
            lista[contador] = row.nombre + ";" + row.cedula + ";" + row.edad + ";"
    + row.pais; }); 
        db.close();
        return cb(null, lists);
}); 
 }
发布评论

评论列表(0)

  1. 暂无评论