【问题标题】:What does visitFunction error mean?visitFunction 错误是什么意思?
【发布时间】:2012-11-26 21:13:04
【问题描述】:

运行 Node.js@0.8.15 + Express@3.0.4 + Jade@0.27.7 + Stylus@0.31.0。 由于某种原因出现以下错误。有人知道这是什么意思吗?

我不认为我在做任何奇怪的事情。这发生在我做的时候:res.render(view, response);

Property 'visitFunction' of object #<Object> is not a function
    at Object.Compiler.visitNode (/app/node_modules/jade/lib/compiler.js:176:32)
    at Object.Compiler.visit (/app/node_modules/jade/lib/compiler.js:161:10)
    at Object.Compiler.visitBlock (/app/node_modules/jade/lib/compiler.js:253:12)
    at Object.Compiler.visitNode (/app/node_modules/jade/lib/compiler.js:176:32)
    at Object.Compiler.visit (/app/node_modules/jade/lib/compiler.js:161:10)
    at Object.Compiler.compile (/app/node_modules/jade/lib/compiler.js:78:10)
    at parse (/app/node_modules/jade/lib/jade.js:101:23)
    at Object.exports.compile (/app/node_modules/jade/lib/jade.js:163:9)
    at Object.exports.render (/app/node_modules/jade/lib/jade.js:215:17)
    at View.exports.renderFile [as engine] (/app/node_modules/jade/lib/jade.js:243:13)

【问题讨论】:

  • 应该是Jade这个版本的bug吧?我将 Jade 版本降级为 0.25,现在可以正常工作了。通过以下方式降级:npm install jade@0.25

标签: javascript node.js express pug


【解决方案1】:

您可能会收到该错误的原因之一是您向Object.prototype 添加了新属性(通常是方法)

示例:

Object.prototype.someNewMethod = function (value1, value2) {
    // ... perform some operations
    return this;
};

不建议使用这种向Object 添加新属性的方式,如问题#1033 中针对快速项目所述。 Object.defineProperty 应与 enumerable 设置为 false 一起使用。

使用Object.defineProperty 扩展Object 的示例

Object.defineProperty(
    Object.prototype, 
    'someNewMethod',
    {
        writable : false,
        // Will not show up in enumerable properties (including for-in loop).
        enumerable : false, 
        configurable : false,
        value : function (value1, value2) {
            // ... perform some operations
            return this;
        }
    }
);

我遇到了完全相同的问题,使用 Object.definePropertyenumerable:false 定义新属性解决了这个问题。

我希望这会有所帮助。

【讨论】:

  • 感谢您简单而出色的回答。但是为什么会这样呢?
猜你喜欢
  • 1970-01-01
  • 2015-03-04
  • 2018-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-19
  • 2018-02-18
  • 2011-01-26
相关资源
最近更新 更多