0

I'm trying to put one csrf into a loop... but this doesn't work for me.

Route

router.get('/:id', ensureAuthenticated, (req, res) => {
    res.render('stories/show', storiesView: stories, 
                             {csrfToken: req.csrfToken()});

});

View

 {{#each storiesView.comments}}

 <a href="#modalOwned" class="waves-effect waves-light btn modal-trigger">HAIL</a>

     
     <div id="modalOwned" class="modal">
         <div class="modal-content">
             <h4>Title</h4>
             <p>sunset of lights {{csrfToken}}</p> 
         </div>
         <div class="modal-footer">
             <a href="" class="modal-close waves-effect waves-green btn-flat">Aggre</a>
         </div>
     </div>

{{/each}}

APP.JS

..code...

app.use(cookieParser());
app.use(csrf({cookie: true}));

If I put csrf outside of loop, this works, but I need the csrf in the loop

Thanks!

Claire Nielsen
  • 1,881
  • 1
  • 14
  • 31
Rioko
  • 35
  • 4
  • Possible duplicate of [Access a variable outside the scope of a Handlebars.js each loop](https://stackoverflow.com/questions/13645084/access-a-variable-outside-the-scope-of-a-handlebars-js-each-loop) – TommyBs Sep 21 '18 at 07:59

1 Answers1

0

To display the csrfToken field from inside the loop you need write it like this:

<p>sunset of lights {{../csrfToken}}</p>

The ../ path segment is used to references the parent template scope.

IronGeek
  • 4,764
  • 25
  • 27