【问题标题】:jade-pdf-redline cannot read property 'length' of undefined node.jsjade-pdf-redline 无法读取未定义 node.js 的属性“长度”
【发布时间】:2016-04-22 09:35:15
【问题描述】:

当我想从我的翡翠模板生成带有翡翠-pdf-redline 的 PDF 时,我遇到了这个错误:

TypeError: Cannot read property 'length' of undefined on line 12
at eval (eval at <anonymous> (/var/www/html/BP_webApp/node_modules/jade-pdf-redline/node_modules/jade/lib/jade.js:160:8), <anonymous>:62:26)
at res (/var/www/html/BP_webApp/node_modules/jade-pdf-redline/node_modules/jade/lib/jade.js:161:38)
at Stream.end (/var/www/html/BP_webApp/node_modules/jade-pdf-redline/lib/index.js:31:18)
at _end (/var/www/html/BP_webApp/node_modules/jade-pdf-redline/node_modules/through/index.js:65:9)
at Stream.stream.end (/var/www/html/BP_webApp/node_modules/jade-pdf-redline/node_modules/through/index.js:74:5)
at Stream.onend (stream.js:59:10)
at emitNone (events.js:85:20)
at Stream.emit (events.js:179:7)
at drain (/var/www/html/BP_webApp/node_modules/jade-pdf-redline/node_modules/through/index.js:34:23)
at Stream.stream.queue.stream.push (/var/www/html/BP_webApp/node_modules/jade-pdf-redline/node_modules/through/index.js:45:5)
at Stream.end (/var/www/html/BP_webApp/node_modules/jade-pdf-redline/node_modules/through/index.js:15:35)
at _end (/var/www/html/BP_webApp/node_modules/jade-pdf-redline/node_modules/through/index.js:65:9)
at Stream.stream.end (/var/www/html/BP_webApp/node_modules/jade-pdf-redline/node_modules/through/index.js:74:5)
at Stream.method [as end] (/var/www/html/BP_webApp/node_modules/jade-pdf-redline/node_modules/duplexer/index.js:47:39)
at ReadStream.onend (_stream_readable.js:491:10)
at ReadStream.g (events.js:273:16)
at emitNone (events.js:85:20)
at ReadStream.emit (events.js:179:7)
at endReadableNT (_stream_readable.js:906:12)
at nextTickCallbackWith2Args (node.js:475:9)
at process._tickCallback (node.js:389:17)

index.js

conn.query("SELECT * FROM tblForm", function(err, rows) {
     if (err) {
        console.log('Error selecting: %s', err);
     }

     for (var i in rows) {
        console.log('Post: ', rows[i]);
     }

     res.render('form', {
        title: 'titlw',
        rows: rows
     });   

     fs.createReadStream('views/form.jade').pipe(jadepdf()).pipe(fs.createWriteStream('form.pdf'));

  });

form.jade

- for(var i = 0, len = rows.length; i < len; i++) {
                div
                    label Programme
                    input(style='float: right; clear: both; width: 300px;' value='#{rows[i].programme}')
                div
                    br
                    label Action Type
                    input(style='float: right; clear: both; width: 300px;' value='#{rows[i].actionType}')
                div
                    br
                    label Call
                    input(style='float: right; clear: both; width: 300px;' value='#{rows[i].callCell}')
                div
                    br
                    label Language
                    input(style='float: right; clear: both; width: 300px;' value='#{rows[i].lang}')
            - }

该 API 无法读取这一行 - for(var i = 0, len = rows.length; i

【问题讨论】:

    标签: node.js pdf-generation pug


    【解决方案1】:

    我认为,您应该在 jadepdf() 函数中传递 rows 变量。

    var options = {
       "locals": {
          "rows": rows  
        }
    };
    
    fs.createReadStream('views/form.jade').pipe(jadepdf(options)).pipe(fs.createWriteStream('form.pdf'));
    

    因为,当jadepdf 尝试将其转换为.pdf 时,jade 模板的空值为rows

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多