我正在尝试将数据从我在我的路径文件中关注的Express应用程序中的sqlite3数据库中获取到网页
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) {
if (exists){
console.log('DB File Exists!!!!');
}
var data = 'Data Starts';
db.serialize(function() {
db.each("SELECT id, as_nr, as_name FROM Autonomous_systems", function(err, row) {
console.log(+row.id+": "+row.as_nr);
//data+= '--'+row.as_name + ": " + row.as_nr;
});
});
db.close();
console.log(data);
res.render('index', { title: 'Express', datax: data });
});
module.exports = router;
当我在浏览器中加载页面时,我首次获取页面并在控制台上打印行。
但是当我重新加载页面时出现以下错误
> sqliteTest@0.0.0 start /home/ehsan/n_ode/Tutorials/sqliteTest
> node ./bin/www
DB File Exists!!!!
Data Starts
29: 25648/53265
30: 5555
31: 5559
35: 98789
36: 98789
GET / 304 80.428 ms - -
GET /stylesheets/style.css 304 3.021 ms - -
重新加载
DB File Exists!!!!
/home/ehsan/n_ode/Tutorials/sqliteTest/node_modules/sqlite3/lib/trace.js:28
throw err;
^
TypeError: Cannot read property 'id' of undefined
at /home/ehsan/n_ode/Tutorials/sqliteTest/routes/index.js:21:23
at Database.errBack (/home/ehsan/n_ode/Tutorials/sqliteTest/node_modules/sqlite3/lib/sqlite3.js:16:21)
at Database.<anonymous> (/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:20:8)
at /home/ehsan/n_ode/Tutorials/sqliteTest/routes/index.js:19: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)
--> in Database#each('SELECT id, as_nr, as_name FROM Autonomous_systems', [Function])
at Database.<anonymous> (/home/ehsan/n_ode/Tutorials/sqliteTest/routes/index.js:20:8)
at /home/ehsan/n_ode/Tutorials/sqliteTest/routes/index.js:19: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
at Function.process_params (/home/ehsan/n_ode/Tutorials/sqliteTest/node_modules/express/lib/router/index.js:330:12)
at next (/home/ehsan/n_ode/Tutorials/sqliteTest/node_modules/express/lib/router/index.js:271:10)
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
答案 0 :(得分:0)
问题在于我已宣布
var db = new sqlite3.Database(file);
全球路线
在使用和查询一次后调用db.close();
一旦db关闭,后续查询将返回undefined
在路径的get方法内移动var db = new sqlite3.Database(file);
会在每次访问时打开一个连接