Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Express+sqlite3 TypeError: Cannot read property #596

Closed
ehsanzaffar opened this issue Feb 7, 2016 · 2 comments
Closed

Express+sqlite3 TypeError: Cannot read property #596

ehsanzaffar opened this issue Feb 7, 2016 · 2 comments

Comments

@ehsanzaffar
Copy link

I am trying to get data onto a web page from an sqlite3 database in my Express app I have following in my route file

var express = require('express');
var router = express.Router();
var fs = require("fs");
var file = "./database/testDB.db";
var exists = fs.existsSync(file);
var sqlite3 = require("sqlite3").verbose();
var db = new sqlite3.Database(file);
/* GET home page. */
router.get('/', function(req, res, next) {
    var data = 'Data Starts';
    db.serialize(function() {
        db.each("SELECT id, as_nr FROM Autonomous_systems", function(err, row) {
            console.log ('--'+row.id + ": " + row.as_nr);
        });
    });
    db.close();
    console.log (data);
    res.render('index', { title: 'Express', datax: data });
});

module.exports = router;

When I load the page in browser I get the page for the first time and the rows printed on console.

but when I reload the page I get following error

Data Starts
--29: 25648/53265
--30: 5555
--31: 5559
GET / 304 76.742 ms - -
GET /stylesheets/style.css 304 4.419 ms - -
/home/ehsan/n_ode/Tutorials/sqliteTest/routes/index.js:24
            console.log ('--'+row.id + ": " + row.as_nr);
                                 ^

TypeError: Cannot read property 'id' of undefined
    at /home/ehsan/lude the following file with any support request:
npm ERR!     /home/ehsan/n_ode/Tutorials/sqliteTest/npm-debug.log
n_ode/Tutorials/sqliteTest/routes/index.js:24:28
    at Database.errBack (/home/ehsan/n_ode/Tutorials/sqliteTest/node_modules/sqlite3/lib/sqlite3.js:16:21)
    at Database.each (/home/ehsan/n_ode/Tutorials/sqliteTest/node_modules/sqlite3/lib/sqlite3.js:20:25)
    at Database.<anonymous> (/home/ehsan/n_ode/Tutorials/sqliteTest/routes/index.js:23:8)
    at /home/ehsan/n_ode/Tutorials/sqliteTest/routes/index.js:22:5
    at Layer.handle [as handle_request] (/home/ehsan/n_ode/Tutorials/sqliteTest/node_modules/express/lib/router/layer.js:95:5)
    at next (/home/ehsan/n_ode/Tutorials/sqliteTest/node_modules/express/lib/router/route.js:131:13)
    at Route.dispatch (/home/ehsan/n_ode/Tutorials/sqliteTest/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/home/ehsan/n_ode/Tutorials/sqliteTest/node_modules/express/lib/router/layer.js:95:5)
    at /home/ehsan/n_ode/Tutorials/sqliteTest/node_modules/express/lib/router/index.js:277:22

npm ERR! Linux 3.16.0-60-generic
npm ERR! argv "/home/ehsan/.nvm/versions/node/v4.2.4/bin/node" "/home/ehsan/.nvm/versions/node/v4.2.4/bin/npm" "start"
npm ERR! node v4.2.4
npm ERR! npm  v2.14.12
npm ERR! code ELIFECYCLE
npm ERR! sqliteTest@0.0.0 start: `node ./bin/www`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the sqliteTest@0.0.0 start script 'node ./bin/www'.
npm ERR! This is most likely a problem with the sqliteTest package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node ./bin/www
npm ERR! You can get their info via:
npm ERR!     npm owner ls sqliteTest
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     /home/ehsan/n_ode/Tutorials/sqliteTest/npm-debug.log

@kroggen
Copy link

kroggen commented May 25, 2016

You must check for error inside the callback on db.each(), like this:

if (err) console.log(err)
else ...

And note that you are closing the database on the first request. Let it open.

@tmcw
Copy link
Contributor

tmcw commented Oct 14, 2016

Closing; the answer from @kroggen is the correct one.

@tmcw tmcw closed this as completed Oct 14, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants