'无法读取未定义'nodeJS的属性'长度'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24550861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'Cannot read property 'length' of undefined' nodeJS
提问by Trigamma
I'm getting started in Node.js and I'm completely confused as to why I can't get the results of my SQL query to render on a page, i'm using sqlite3. This is a part of my index.js file
我刚开始使用 Node.js,我完全不明白为什么我无法将 SQL 查询的结果呈现在页面上,我使用的是 sqlite3。这是我的 index.js 文件的一部分
router.get('/students', function (req, res, module) {
var fs = require("fs");
var file = "./test.db";
var exists = fs.existsSync(file);
if (!exists) {
console.log("Creating DB file.");
fs.openSync(file, "w");
}
var sqlite3 = require("sqlite3").verbose();
var db = new sqlite3.Database(file);
db.serialize(function () {
if (!exists) {
db.run("Create table students ( ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE, Nom varchar (255), Prenom varchar (255) );");
}
});
db.all('SELECT * FROM students', function selectCb(err, rows, fields) {
if (err) {
throw err;
}
for (var i in rows) {
console.log(rows[i]);
}
res.render('showResults.jade', {
results: rows
});
});
db.close();
res.render('students', { title: 'list' });
});
This is my Jade (create.jade) file;
这是我的 Jade (create.jade) 文件;
block content
form(method='get', action='/students/create')
button(type='submit') Creer
ul
each item, i in results
li #{item.Nom} (#{item.Prenom})
li= JSON.stringify(item)
I put all of this things in my express app, I launch it with my shell, I receive all the sql data like this:
我把所有这些东西都放在我的 express 应用程序中,我用我的 shell 启动它,我收到所有这样的 sql 数据:
{ ID: 1, Nom: 'zeze', Prenom: 'zeze' }
{ ID: 2, Nom: 'ertty', Prenom: 'uuuuuuuuuuu' }
But my /students page show a message
但是我的 /students 页面显示一条消息
http://i.stack.imgur.com/ormUG.png
http://i.stack.imgur.com/ormUG.png
Could you please help me, I'm really desperate. (Sorry for my bad english I'm French) ^^
你能帮我吗,我真的很绝望。(对不起,我的英语不好,我是法国人)^^
回答by paradite
res.render('students', { title: 'list' });
and this line
和这条线
each item, i in results
The variable names do not match.
变量名称不匹配。
From the picture, I can see that you are rendering the page called students.jade. For that page, you are sending variable titleto Jade but you declared that the variable would be called resultsin Jade.
从图片中,我可以看到您正在渲染名为students.jade. 对于该页面,您正在向titleJade发送变量,但您声明将results在 Jade 中调用该变量。
回答by Emanuel Saringan
Have you tried removing the second res.rendercall?
您是否尝试删除第二个res.render电话?
It is possible that it is the one being called instead of the first one since the async call to the DB is not yet finished when the execution reaches the second res.rendercall.
有可能它是被调用的而不是第一个,因为当执行到达第二个res.render调用时,对数据库的异步调用尚未完成。
回答by Ben
db.close() should be moved inside the query's callback because it closes the db connection before the query has the chance to finish. Here is the revised code:
db.close() 应该在查询的回调中移动,因为它会在查询有机会完成之前关闭数据库连接。这是修改后的代码:
db.all('SELECT * FROM students', function selectCb(err, rows, fields) {
if (err) {
db.close(); // <<<<<<<<<<<<<<
throw err;
}
for (var i in rows) {
console.log(rows[i]);
}
res.render('showResults.jade', {
results: rows
});
db.close(); // <<<<<<<<<<
});

