node.js - 列出文件时,node.js 出现类型错误

标签 node.js pug

我有一个目录

view/tasks/index.jade 

我想列出里面的所有文件,最初我有一个node.js文件的模板,它将字符串添加到任务中,任务页面将输出到目前为止输入的所有字符串,但是现在我想修改它,以便列出任务目录中除了index.jade和其他一些文件之外的所有文件:我将列出所有其他文件。当我运行下面的修改时,它返回类型错误。任何提示将不胜感激

app.get('/tasks', function(req, res){
  Task.find({}, function (err, docs) {
    res.render('tasks/index', {
      title: 'Tasks index view',
      docs: docs
     });
  });
  // list file mod
   fs.readdir('tasks/index', function(err, data){
    res.render('tasks/index', {"files": data});
   });
  // list file mod
});

下面是我的index.jade 文件

h1 Your tasks
p
  a(href='/tasks/new', class='btn primary') Add a Task


- if(docs.length)
  table
    tr
      th Task
      th
      each task in docs
        tr
          td #{task.task}
          td
            a.btn(href="/tasks/#{task.id}/edit") Edit
- else
   p You don't have any tasks! 


// list all the file
ul
  for file in files
    li
       p= file

下面是我的错误

TypeError: /path/views/tasks/index.jade:22
    20| // list all the file
    21| ul
  > 22|   for file in files
    23|     li
    24|       p= file
    25| 

Cannot read property 'length' of undefined
    at eval (eval at <anonymous> (path/node_modules/jade/lib/jade.js:166:8),     <anonymous>:174:31)
    at eval (eval at <anonymous> (/path/node_modules/jade/lib/jade.js:166:8), <anonymous>:217:4)
    at Object.<anonymous> (path/node_modules/jade/lib

附加错误:

500 TypeError: /path/views/tasks/index.jade:22 20| // list all the file 21| ul > 22|  for file in files 23| li 24| p= file 25| Cannot read property 'length' of undefined

    20| // list all the file
    21| ul
    > 22| for file in files
    23| li
    24| p= file
    25|
    Cannot read property 'length' of undefined
    at eval (eval at (/path/node_modules/jade/lib/jade.js:166:8), :174:31)
    at eval (eval at (/path/node_modules/jade/lib/jade.js:166:8), :217:4)
    at Object. (/path/node_modules/jade/lib/jade.js:167:35)
    at ServerResponse.res._render (/path/node_modules/express/lib/view.js:422:21)

更新代码:

var async = require( 'async' );

app.get('/tasks', function(req, res){
  var tasks, files;

  async.series( [

    // Find tasks
    function ( callback ) {
      Task.find({}, function (err, docs) {
        tasks = docs;

        callback();
      });
    },

    // Find files
    function ( callback ) {
     fs.readdir('tasks/index', function(err, data){
      files = data;

      callback();
     });
    }

  // Render and send response
  ], function ( error, results ) {
        res.render('tasks/index', {
          title: 'Tasks index view',
          docs: tasks,
          files: files
         });
  } );

});


app.get('/tasks/new', function(req, res){
  res.render('tasks/new.jade', {
    title: 'New task view'
  });
});

app.post('/tasks', function(req, res){
  var task = new Task(req.body.task);
  task.save(function (err) {
    if (!err) {
      res.redirect('/tasks');
    }
    else {
      res.redirect('/tasks/new');
    }
  });
});

app.get('/tasks/:id/edit', function(req, res){
  Task.findById(req.params.id, function (err, doc){
    res.render('tasks/edit', {
      title: 'Edit Task View',
      task: doc
    });
  });
});

app.put('/tasks/:id', function(req, res){
  Task.findById(req.params.id, function (err, doc){
    doc.updated_at = new Date();
    doc.task = req.body.task.task;
    doc.save(function(err) {
      if (!err){
        res.redirect('/tasks');
      }
      else {
        // error handling
      }
    });
  });
});

最佳答案

看起来由于同时进行两个异步操作而存在竞争条件。 npm install async 并尝试以下操作:

var async = require( 'async' );

app.get('/tasks', function(req, res){
  var tasks, files;

  async.parallel( [

    // Find tasks
    function ( callback ) {
      Task.find({}, function (err, docs) {
        tasks = docs;

        callback();
      });
    },

    // Find files
    function ( callback ) {
     fs.readdir('tasks/index', function(err, data){
      files = data;

      callback();
     });
    }

  // Render and send response
  ], function ( error, results ) {
        res.render('tasks/index', {
          title: 'Tasks index view',
          docs: tasks,
          files: files
         });
  } );

});

关于node.js - 列出文件时,node.js 出现类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18342643/

相关文章:

node.js - Express res header 事件不再触发

javascript - 如何在express js中编写包装类

node.js - 如何在 Mongoose 中同时更新对象并插入数组

node.js - Node 、 express 、 Jade : How to process form data

html - 使用 Node.js 发送 POST JSON 数据并接收(无 BODYPARSER)

javascript - npm scripts nodemon - 观察 js 和 scss 文件的变化

angularjs - 在Jade中转义[方括号]以防止与AngularJS冲突?

javascript - 简单的 db mongo 查询出错了?

javascript - 尝试使用 multer node.js 上传时出现 404 错误

javascript - Jade 表单元格内的按钮