渲染dust.js模板时出现TypeError

时间:2013-11-13 21:29:50

标签: javascript node.js mongodb mongoose dust.js

我有一个dust.js模板,它应该呈现来自mongodb集合的JSON对象。

{
  "author": "Leslie Horn",
  "title": "An Awesome 132-Cube Ice Tray Exists Because People Are Assholes",
  "published": "2013-06-13T21:00:00.000Z",
  "link": "http://gizmodo.com/an-awesome-132-cube-ice-tray-exists-because-people-are-513229668",
  "feed": {
    "link": {
      "xml": "feed/http://feeds.gawker.com/gizmodo/excerpts.xml",
      "html": "http://gizmodo.com"
    },
    "title": "Gizmodo"
  },
  "summary": "<p><img alt=\"An Awesome 132-Cube Ice Tray Exists Because People Are Assholes\" height=\"360\" width=\"640\" src=\"http://img.gawkerassets.com/img/18qq51qya5ckrpng/ku-xlarge.png\"></p> <p>First, there's a special place in hell for people who leave the ice trays empty. Ugh! But those people exist, so thankfully <a href=\"http://www.lekueusa.com/product-search/Ice-Box-White-plu0250400B01C002.html\">Lékué makes a tray</a> that has space to store 132 cubes. </p><p><a href=\"http://gizmodo.com/an-awesome-132-cube-ice-tray-exists-because-people-are-513229668\">Read more...</a></p><img width=\"1\" height=\"1\" src=\"http://gizmodo.feedsportal.com/c/34976/f/647164/s/2d41b89d/mf.gif\" border=\"0\"><div><table border=\"0\"><tr><td valign=\"middle\"><a href=\"http://share.feedsportal.com/share/twitter/?u=http%3A%2F%2Fgizmodo.com%2Fan…668&amp;t=An+Awesome+132-Cube+Ice+Tray+Exists+Because+People+Are+Assholes\"><img src=\"http://res3.feedsportal.com/social/twitter.png\" border=\"0\"></a> <a href=\"http://share.feedsportal.com/share/facebook/?u=http%3A%2F%2Fgizmodo.com%2Fa…668&amp;t=An+Awesome+132-Cube+Ice+Tray+Exists+Because+People+Are+Assholes\"><img src=\"http://res3.feedsportal.com/social/facebook.png\" border=\"0\"></a> <a href=\"http://share.feedsportal.com/share/linkedin/?u=http%3A%2F%2Fgizmodo.com%2Fa…668&amp;t=An+Awesome+132-Cube+Ice+Tray+Exists+Because+People+Are+Assholes\"><img src=\"http://res3.feedsportal.com/social/linkedin.png\" border=\"0\"></a> <a href=\"http://share.feedsportal.com/share/gplus/?u=http%3A%2F%2Fgizmodo.com%2Fan-a…668&amp;t=An+Awesome+132-Cube+Ice+Tray+Exists+Because+People+Are+Assholes\"><img src=\"http://res3.feedsportal.com/social/googleplus.png\" border=\"0\"></a> <a href=\"http://share.feedsportal.com/share/email/?u=http%3A%2F%2Fgizmodo.com%2Fan-a…668&amp;t=An+Awesome+132-Cube+Ice+Tray+Exists+Because+People+Are+Assholes\"><img src=\"http://res3.feedsportal.com/social/email.png\" border=\"0\"></a></td></tr></table></div><br><br><a href=\"http://da.feedsportal.com/r/165665057117/u/49/f/647164/c/34976/s/2d41b89d/kg/342-363/a2.htm\"><img src=\"http://da.feedsportal.com/r/165665057117/u/49/f/647164/c/34976/s/2d41b89d/kg/342-363/a2.img\" border=\"0\"></a><img width=\"1\" height=\"1\" src=\"http://pi.feedsportal.com/r/165665057117/u/49/f/647164/c/34976/s/2d41b89d/kg/342-363/a2t.img\" border=\"0\"><div>\n<a href=\"http://feeds.gawker.com/~ff/gizmodo/excerpts?a=iGWw_D7Zjj0:Xf9e--TXu90:yIl2AUoC8zA\"><img src=\"http://feeds.feedburner.com/~ff/gizmodo/excerpts?d=yIl2AUoC8zA\" border=\"0\"></a> <a href=\"http://feeds.gawker.com/~ff/gizmodo/excerpts?a=iGWw_D7Zjj0:Xf9e--TXu90:qj6IDK7rITs\"><img src=\"http://feeds.feedburner.com/~ff/gizmodo/excerpts?d=qj6IDK7rITs\" border=\"0\"></a> <a href=\"http://feeds.gawker.com/~ff/gizmodo/excerpts?a=iGWw_D7Zjj0:Xf9e--TXu90:V_sGLiPBpWU\"><img src=\"http://feeds.feedburner.com/~ff/gizmodo/excerpts?i=iGWw_D7Zjj0:Xf9e--TXu90:V_sGLiPBpWU\" border=\"0\"></a> <a href=\"http://feeds.gawker.com/~ff/gizmodo/excerpts?a=iGWw_D7Zjj0:Xf9e--TXu90:dnMXMwOfBR0\"><img src=\"http://feeds.feedburner.com/~ff/gizmodo/excerpts?d=dnMXMwOfBR0\" border=\"0\"></a>\n</div><img src=\"http://feeds.feedburner.com/~r/gizmodo/excerpts/~4/iGWw_D7Zjj0\" height=\"1\" width=\"1\">",
  "_id": "528384f6ce53a99468000006",
  "__v": 0
}

问题是,在渲染对象时我总是遇到TypeError。

Uncaught TypeError: Object #<h> has no method '_get' 

使用grunt-dustjs在服务器上预编译模板。

<article class="article">
    <div class="article-feed">
        <p class="published">{published}</p>
        <h1><a href="{feed.link.html}" target="_blank">{feed.title|s}</a></h1>
    </div>
    <h2><a href="{link}" target="_blank">{title|s}</a></h2>
    {?content}
        <div class="article-content">{content|s}</div>
    {:else}
        {?summary}
        <div class="article-summary">{summary|s}</div>
        {/summary}
    {/content}
</article>

编译的模板看起来像这样

(function() {
    dust.register("article", body_0);
    function body_0(chk, ctx) {
        return chk.write("<article class=\"article\"><div class=\"article-feed\"><p class=\"published\">").reference(ctx._get(false, ["published"]), ctx, "h").write("</p><h1><a href=\"").reference(ctx._get(false, ["feed", "link"]), ctx, "h").write("\" target=\"_blank\">").reference(ctx._get(false, ["feed", "title"]), ctx, "h", ["s"]).write("</a></h1></div><h2><a href=\"").reference(ctx._get(false, ["link"]), ctx, "h").write("\" target=\"_blank\">").reference(ctx._get(false, ["title"]), ctx, "h", ["s"]).write("</a></h2>").exists(ctx._get(false, ["content"]), ctx, {"else": body_1,"block": body_3}, null).write("</article>");
    }
    function body_1(chk, ctx) {
        return chk.exists(ctx._get(false, ["summary"]), ctx, {"block": body_2}, null);
    }
    function body_2(chk, ctx) {
        return chk.write("<div class=\"article-summary\">").reference(ctx._get(false, ["summary"]), ctx, "h", ["s"]).write("</div>");
    }
    function body_3(chk, ctx) {
        return chk.write("<div class=\"article-content\">").reference(ctx._get(false, ["content"]), ctx, "h", ["s"]).write("</div>");
    }
    return body_0;
})();

遗憾的是,我在网上找不到任何解决方案。我在mongodb中尝试过精简查询,这也不起作用。

奇怪的是它之前有效,但是由于我改变了我的数据库模式,它就停止了工作。

1 个答案:

答案 0 :(得分:2)

您使用什么版本的Dust进行渲染。看起来您使用2.2.0进行编译,但使用早期版本进行渲染。 Dust内部API已更改为2.2.0(已编译的Dust中的getgetPath已替换为_get)。在{2.2}中添加了_get方法,因此如果您使用较旧版本的Dust进行渲染,则会出现此错误。