Javascript 车把:首次运行时未找到部分车把

Javascript 车把:首次运行时未找到部分车把,javascript,node.js,handlebars.js,Javascript,Node.js,Handlebars.js,我有一个使用Handlebar编译模板的节点脚本。这是我的模板: <div class="header"> <h1>{{title}}</h1> </div> <div class="body"> <p>{{body}}</p> </div> <div class="footer"> <div><a href="http://twitter.com

我有一个使用Handlebar编译模板的节点脚本。这是我的模板:

<div class="header">
    <h1>{{title}}</h1>
</div>
<div class="body">
    <p>{{body}}</p>
</div>
<div class="footer">
    <div><a href="http://twitter.com/{{author.twitter}}">{{autor.name}}</a>
    </div>
    <ul>
      {{#each tags}}
        <li>{{this}}</li>
      {{/each}}
    </ul>
    {{> example_partial}}
</div>
当我第一次使用
node app.js
通过node运行脚本时,我不能真正理解的是,我得到了以下错误:

/Users/rahul/stencil/node_modules/handlebars/dist/cjs/handlebars/runtime.js:266
    throw new _exception2['default']('The partial ' + options.name + ' could not be found');
    ^
Error: The partial example_partial could not be found
    at Object.invokePartial (/Users/rahul/stencil/node_modules/handlebars/dist/cjs/handlebars/runtime.js:266:11)
    at Object.invokePartialWrapper [as invokePartial] (/Users/rahul/stencil/node_modules/handlebars/dist/cjs/handlebars/runtime.js:68:39)
    at Object.eval (eval at createFunctionContext (/Users/rahul/stencil/node_modules/handlebars/dist/cjs/handlebars/compiler/javascript-compiler.js:254:23), <anonymous>:16:28)
    at main (/Users/rahul/stencil/node_modules/handlebars/dist/cjs/handlebars/runtime.js:173:32)
    at ret (/Users/rahul/stencil/node_modules/handlebars/dist/cjs/handlebars/runtime.js:176:12)
    at ret (/Users/rahul/stencil/node_modules/handlebars/dist/cjs/handlebars/compiler/compiler.js:525:21)
    at /Users/rahul/stencil/examples/standalone_v1/handlebars-example.js:29:14
    at tryToString (fs.js:414:3)
    at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:401:12)
/Users/rahul/stencil/node_modules/handlebar/dist/cjs/handlebar/runtime.js:266
抛出新的_exception2['default']('The partial'+options.name+'找不到”);
^
错误:找不到部分示例\u partial
位于Object.invokePartial(/Users/rahul/stencil/node_modules/handlebar/dist/cjs/handlebar/runtime.js:266:11)
在Object.invokePartialWrapper[作为invokePartial](/Users/rahul/stencil/node_modules/handlebar/dist/cjs/handlebar/runtime.js:68:39)
在Object.eval(在createFunctionContext(/Users/rahul/stencil/node_modules/handlebar/dist/cjs/handlebar/compiler/javascript compiler.js:254:23)进行eval),16:28
主要(/Users/rahul/stencil/node_modules/handlebar/dist/cjs/handlebar/runtime.js:173:32)
在ret(/Users/rahul/stencil/node_modules/handlebar/dist/cjs/handlebar/runtime.js:176:12)
在ret(/Users/rahul/stencil/node_modules/handlebar/dist/cjs/handlebar/compiler/compiler.js:525:21)
at/Users/rahul/stencil/examples/standalone_v1/handlebar example.js:29:14
在tryToString(fs.js:414:3)
在FSReqWrap.readFileAfterClose[完成时](fs.js:401:12)

但是,当我再次运行该程序时,它工作正常,并且我得到了预期的输出(没有任何更改)。有人能给我解释一下我做错了什么吗

问题是,当您开始编译使用它的模板时,您的分部代码实际上没有注册。这是因为
fs.readFile
是一个异步操作

一种解决方案是使用
fs.readFileSync

var partial = fs.readFileSync('handlebars-example-partial.html', 'utf-8'); 
handlebars.registerPartial('example_partial', partial);

fs.readFile('handlebars-example.html', 'utf-8', function(error, source){
  var template = handlebars.compile(source);                                                                                                                                                                                                 
  var html = template(data);                                                                                                                                                                                                                 
  console.log(html)
});
或者,您可以将其全部放在注册分部的回调中:

fs.readFile('handlebars-example-partial.html', 'utf-8', function(error, partial) {
  handlebars.registerPartial('example_partial', partial);
  fs.readFile('handlebars-example.html', 'utf-8', function(error, template) {
    var compiled = handlebars.compile(template);
    var html = compiled(data);
    console.log(html);
  });
}

您如何使错误恢复?我已经用你的代码启动了一个小的repo,但是在看到错误(实际上发生在第二次编译时)后,我无法恢复它。嘿@adam beck,如果我编辑js文件,它通常会为我恢复。谢谢,现在我知道出了什么问题。上面代码中的一个小更正(对于任何复制粘贴它的人;)
handlebar.registerPartial('example_partial',source)应该是
把手。注册部分('example_partial',partial)
var partial = fs.readFileSync('handlebars-example-partial.html', 'utf-8'); 
handlebars.registerPartial('example_partial', partial);

fs.readFile('handlebars-example.html', 'utf-8', function(error, source){
  var template = handlebars.compile(source);                                                                                                                                                                                                 
  var html = template(data);                                                                                                                                                                                                                 
  console.log(html)
});
fs.readFile('handlebars-example-partial.html', 'utf-8', function(error, partial) {
  handlebars.registerPartial('example_partial', partial);
  fs.readFile('handlebars-example.html', 'utf-8', function(error, template) {
    var compiled = handlebars.compile(template);
    var html = compiled(data);
    console.log(html);
  });
}