Search code examples
apostrophe-cmsnunjucks

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


I have Apostrophe CMS project running in the production. Recently I've updated the app and also updated Apostrophe CMS version. After this I repeatedly get this error:

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

The thing is that I can't inspect where this error is coming from. Also this happened very randomly. In the dev server I don't have this issue and can not replicate in order to find the root cause. After the above error it shows also Nunjucks error lines where the error supposed to happened, but that error lines are useless, because if I check the template there is nothing written in the specified error line.

I've checked the Apostrophe CMS source code. It throws the error here:

...
// 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.');
      }
...

In my app code, I haven't used the render() function anywhere. NOTE that I randomly get this error in any kind of pages. There are several pieces pages I've created, where get the error too. Even in notFound.html template (where I have no any widgets, it just extends the layout.html) I get the error there too.

Updates

Based on the Nunjucks error lines I found that the error happens on my my-pieces-pages/views/show.html template at the below line:

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

where picture field in the piece has the following look:

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

Solution

  • There was discussion of this in a github issue as well. Eventually it was determined that all of those who experienced this issue were using early releases of Node 12.x.

    The issue is not reproducible with the latest version of Node 12.x according to everyone who has made the attempt.

    So, upgrading to the latest minor and patchlevel release of Node 12.x is the answer to this question. However if a means to reproduce it with the latest Node 12.x release is found steps to reliably reproduce it should be submitted via github and the ticket can be reopened.

    For the sake of completeness it should also be noted that template code should never attempt to execute async functions or functions that take callbacks. I understand that this was not happening in your case.