【问题标题】:Handlebars.compile throws an exception 'Error: You must pass a string or Handlebars AST to Handlebars.compile. You passed <html>...'Handlebars.compile 抛出异常'错误:您必须将字符串或 Handlebars AST 传递给 Handlebars.compile。你通过了 <html>...'
【发布时间】:2018-06-04 22:09:25
【问题描述】:

前提

我们在后端 nodejs 应用程序中运行了把手,用于对发送的各种消息进行模板化。

Handlebars.compile 抛出此异常(从部分编译模板时)

Error: You must pass a string or Handlebars AST to Handlebars.compile. You passed <html>
<head>
... extremely long markup
at Object.compile (/Users/guscrawford/rollick-management-console/deployd/node_modules/handlebars/dist/cjs/handlebars/compiler/compiler.js:501:11)
at HandlebarsEnvironment.hb.compile (/Users/guscrawford/rollick-management-console/deployd/node_modules/handlebars/dist/cjs/handlebars.js:39:40)
at Object.invokePartialWrapper [as invokePartial] (/Users/guscrawford/rollick-management-console/deployd/node_modules/handlebars/dist/cjs/handlebars/runtime.js:71:44)
... additional stack trace through to dpd, bluebird etc.

无法通过隔离复制

继续尝试设置废品项目: yarn add handlebars handlebars-helper-ternary handlebars-helpers handlebars.numeral

然后在nodejs中运行这个脚本:

const   handlebars = require('handlebars'),
        numeralHelper = require('handlebars.numeral'),    
        ternaryHelper = require('handlebars-helper-ternary'),
        helpers = require('handlebars-helpers')({
        handlebars: handlebars
    });
console.log(`Testing...`);
const base = `
<html>
    <body style="font-family:'Segoe UI', Tahoma, Geneva, Verdana,     'sans-serif'; font-size: larger;">
    {{>@partial-block }}
    <td style="text-align: center; padding: 24px;">
    Copyright 2018 My Company, Inc. All rights reserved.

    </body>
</html>

`;
const inner = `
{{#>base}}
    {{subscriber.name}},

    {{member.name}} has received a notifier from {{subscriber.name}}.    

    Click the link below to review!. 
    <a href='{{link}}'>Go!</a>

    Thank you,
    My Company
{{/base}}
`;
numeralHelper.registerHelpers(handlebars);
handlebars.registerHelper('ternary', ternaryHelper);
handlebars.registerHelper("moduloIf", function (index_count, mod, block)     {

    if (index_count > 0 && parseInt(index_count) % (mod) === 0) {
        return block.fn(this);
    } else {
        return block.inverse(this);
    }
});

handlebars.registerHelper("substitute", function(a, options) {
  try {
    function index(obj,i) { return obj ? obj[i] : {} }
    let data = a.split('.').reduce(index, this);
    if (data && typeof data === 'string') return data;
    else return options.fn(this);
  } catch (e) {
    console.log('substitute helper:' + e);
  }
});
handlebars.registerPartial('base',base)
var output = handlebars.compile(inner)({name:'Gus'});
console.log('Output:');
console.log(output)

进一步考虑

实际上,我们将车把require 包装在另一个模块中,代码针对车把实例运行,如示例脚本所示。我们正在导出车把实例。

【问题讨论】:

    标签: javascript node.js handlebars.js deployd


    【解决方案1】:

    字符串是一个缓冲区

    尽管记录了typeof 我作为字符串 传递的模板字符串,readFileAsync 的输出没有传递编码是一个原始节点缓冲区。

    【讨论】:

    • 我告诉过你,它不是字符串,Buffer 也不是字符串。因此我的回答是正确的,错误发生在你没有显示的代码部分,我告诉你你的错误是什么。
    • 正如我所描述的,它正是一个实现包含原始字符串的toString 方法的对象。
    【解决方案2】:

    错误很明显,您传递的不是字符串或 AST。

    This 是车把抛出该错误的唯一方法。

    if (input == null || (typeof input !== 'string' && input.type !== 'Program')) {
        throw new Exception('You must pass a string or Handlebars AST to Handlebars.compile. You passed ' + input);
    }
    

    您可能通过toString 方法传递了object,这就是您看到的原因:

    You passed <html>
    <head>
    ... extremely long markup
    

    const input = {
      toString() {
        return `<html>
        <head>`;
      }
    }
    console.log('You must pass a string or Handlebars AST to Handlebars.compile. You passed ' + input);

    【讨论】:

    • 我记录了一个typeof handlebars.compile(inner),它肯定是一个字符串
    • 除非它被覆盖,否则大多数toString() 调用将输出[Object] 否?
    • 你确定它在哪里触发错误?我没有看到完整的堆栈跟踪,所以我会说你错了。是的,除非被覆盖,否则它将是 [Object object]
    • 一种调试方法是修改我链接的那段代码,在node_modules/handlebars... 中输入console.log 并准确查看您发送的内容。但是错误很明显,您发送的内容 100% 不是字符串。
    • 1. 100% 传递给compile 的模板的console.log 输出是一个字符串。 2. 我意识到我为了简洁而删除了标记,但堆栈跟踪显然始于 Object.compile &gt; HandlebarsEnvironment.hb.compile &gt; Object.invokePartialWrapper &gt; Object.eval (functions in the stack outside of this I'm pretty confident are not in the scope of handlebars)
    猜你喜欢
    • 2014-08-12
    • 1970-01-01
    • 2022-11-09
    • 2022-01-17
    • 2015-03-28
    • 2017-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多