[英]Is this the way to list a file in directory using node.js and jade
I am trying to list a file in a directory using jade and node.js but I am not sure if below is the right way of doing it, as I receive a type error that says 我试图使用jade和node.js在目录中列出一个文件,但我不确定下面是否是正确的方法,因为我收到一个类型错误,说
Cannot read property 'length' of undefined
Any hint would be much appreciated 任何提示都将非常感激
h1 Your tasks
p
// list all the file
ul
for file in files
li
p= file
node.js code node.js代码
app.get('/tasks', function(req, res){
fs.readdir('tasks/index', function(err, data){
res.render('tasks/index', {"files": data});
});
});
update 更新
app.get('/tasks', function(req, res){
fs.readdir('tasks', function(err, data){
res.render('tasks/index', {"files": data});
});
});
error 错误
500 TypeError: /path/views/tasks/index.jade:7 5| // list all the file 6| ul > 7| each file in files 8| li 9| p= file 10| Cannot read property 'length' of undefined
5| // list all the file
6| ul
> 7| each file in files
8| li
9| p= file
10|
Cannot read property 'length' of undefined
OK, several issues: 好的,有几个问题:
each file in files
instead of for
each file in files
,而不是for
readDir
call is probably throwing an error which is why data
(and thus files
) is undefined. readDir
调用可能会引发错误,这就是data
(以及files
)未定义的原因。 fs.readdir(__dirname + '/views/tasks', function(error, data){
if (error) {
res.status(500).send(error);
return;
}
res.render('tasks/index', {"files": data});
});
I don't know your filesystem organization entirely, so the paths are just guesses, but the root of your problem seems to be not coding properly in terms of filesystem organization. 我完全不知道你的文件系统组织,所以路径只是猜测,但问题的根源似乎是在文件系统组织方面没有正确编码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.