类型渲染灰尘时出错.js模板

TypeError when rendering dust.js template

本文关键字:出错 js 模板 类型      更新时间:2023-09-26

我有一个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 中尝试了精益查询,但也没有用。

奇怪的是它以前工作过,但是自从我更改了数据库架构以来,它就停止了工作。

您使用哪个版本的 Dust 进行渲染。看起来您正在使用 2.2.0 进行编译,但使用早期版本进行渲染。Dust 内部 API 在 2.2.0 中发生了变化(getgetPath 在编译的 Dust 中被替换为 _get)。_get 方法是在 2.2.0 中添加的,因此如果您使用旧版本的 Dust 进行渲染,则会收到此错误。