首页 > 解决方案 > ApostropheCMS - 错误:不能从嵌套在另一个对 render() 调用中的 Nunjucks 辅助函数调用 render()

问题描述

我在生产中运行了撇号 CMS 项目。最近我更新了应用程序并更新了 Apostrophe CMS 版本。在此之后,我反复收到此错误:

Error: render() must not be called from a Nunjucks helper function nested inside another call to render(). Use partial() instead.

问题是我无法检查此错误的来源。这也是非常随机发生的。在开发服务器中,我没有这个问题并且无法复制以找到根本原因。在上述错误之后,它还显示了应该发生错误的 Nunjucks 错误行,但该错误行是无用的,因为如果我检查模板,则指定的错误行中没有任何内容。

我检查了撇号 CMS 源代码。它在这里抛出错误:

...
// Implements `render` and `renderString`. See their
// documentation.

    self.renderBody = function(req, type, s, data, module) {
      if (self.contextReq && (req !== self.contextReq)) {
        throw new Error('render() must not be called from a Nunjucks helper function nested inside another call to render(). Use partial() instead.');
      }
...

在我的应用程序代码中,我没有在render()任何地方使用过该功能。请注意,我在任何类型的页面中都会随机收到此错误。我创建了几个页面,其中也出现了错误。即使在notFound.html模板中(我没有任何小部件,它只是扩展了layout.html)我也得到了错误。

更新

根据 Nunjucks 错误行,我发现错误发生在我的my-pieces-pages/views/show.html模板上,位于以下行:

{{ apos.singleton(piece, 'picture', 'apostrophe-images') }}
...

作品中picture的字段具有以下外观:

{
      name: "picture",
      label: "Picture",
      type: "singleton",
      widgetType: "apostrophe-images",
      options: {
        limit: 1
      }
    },
...

标签: apostrophe-cmsnunjucks

解决方案


在 github 问题中也对此进行了讨论。最终确定所有遇到此问题的人都在使用 Node 12.x 的早期版本。

根据尝试过的每个人的说法,最新版本的 Node 12.x 无法重现该问题。

因此,升级到 Node 12.x 的最新次要和补丁级别版本就是这个问题的答案。但是,如果找到使用最新 Node 12.x 版本重现它的方法,则应通过 github 提交可靠重现它的步骤,并且可以重新打开票证。

为了完整起见,还应注意模板代码不应尝试执行异步函数或接受回调的函数。我了解您的情况没有发生这种情况。


推荐阅读