ApostropheCMS - Error: render() must not be called from a Nunjucks helper function nested inside another call to render()

ApostropheCMS - Error: render() must not be called from a Nunjucks helper function nested inside another call to render()

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

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

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

我已经检查了 Apostrophe 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
      }
    },
...

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

的早期版本

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

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

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