Handlebars.compile抛出异常'错误:您必须将字符串或Handlebars AST传递给Handlebars.compile。你通过了......'

时间:2018-06-04 12:33:34

标签: javascript node.js handlebars.js deployd

前提

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

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

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包装在另一个模块中,代码针对把手实例运行,如示例脚本所示。我们正在导出把手实例。

2 个答案:

答案 0 :(得分:1)

字符串是缓冲区

尽管我将作为字符串传递的模板字符串记录为typeof,但readFileAsync的输出未传递编码是原始节点缓冲区。

<强>咄

答案 1 :(得分:0)

错误很明显,你传递的不是字符串或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);
}

您可能会使用object方法传递toString,这就是您看到的原因:

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);