Well you’ve got a scoping problem I’d say.
var next =... inside a function sticks it into the scope of said function. That means it can’t be accessed outside of the function it was declared.
What I’d do is return the value of
next in the function so it can be passed as an object to another.