Increase value + 1 within for loop in CoffeeScript? -


i tried this:

posts = posts.find({},   sort:     position: 1 ).fetch()  post in posts   = 1   posts.update post._id,     $set:       position:   i++ 

but posts end position 1. want 1, 2, 3, 4, etc.

what i'm doing wrong?

you can force i evaluated (rather referenced) @ right time using do. fine manual:

when using javascript loop generate functions, it's common insert closure wrapper in order ensure loop variables closed over, , generated functions don't share final values. coffeescript provides do keyword, invokes passed function, forwarding arguments.

this situation you're facing. can let coffeescript handle loop counter using for e, in ... form of for:

for post, in posts   (post, i) ->     posts.update post._id,       $set:         position: + 1 

Comments

Popular posts from this blog

javascript - How to name a jQuery function to make a browser's back button work? -

python - Django-easy-pdf: xhtml2pdf reporting reportlab 2.2+ is required, but 3.0 installed -

collision detection - C++ library for mesh to mesh intersection: what is available? -