【问题标题】:module "require" inside a Jade fileJade 文件中的模块“require”
【发布时间】:2015-01-19 23:22:38
【问题描述】:

我需要在 Jade 文件中使用“fs”模块。没有其他 JS 文件。

当我尝试时:

- var js = require('fs')
- var downloadfiles = readdirSync("download") 
for f in downloadfiles
  url
    loc= D + "/download" + f
    lastmod= buildDate
    changefreq daily
    priority 1.0

我收到错误“未定义不是函数”

【问题讨论】:

标签: node.js pug


【解决方案1】:

两个问题

您应该将函数作为参数发送到 Jade 文件,而不是尝试在 Jade ie 中要求它。

var js = require('fs');
res.render('myjadefile', {fsFunction: fs});

myjadefile.jade

- var downloadfile = fsFunction.readdirSync('download');
for f in downloadfiles
   // rest of your code


此外,在第 2 行中,您调用函数“readdirSync”而不在任何地方定义它。应该是

fs.readdirSync

【讨论】:

    【解决方案2】:
    res.render(index, {data:data,
                       title:'Jade Require'
                       require:require})
    

    现在在你的玉里。

    extends layout
    !{fs=require('fs')}
    
    
    body
      - var downloadfiles = readdirSync("download") 
      for f in downloadfiles
        url
          loc= D + "/download" + f
          lastmod= buildDate
          changefreq daily
          priority 1.0
    

    【讨论】:

      猜你喜欢
      • 2019-07-01
      • 2019-03-27
      • 2017-03-01
      • 1970-01-01
      • 2018-11-25
      • 2019-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多