Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Node push Variables dynamic

Node push Variables dynamic


it works very well until I do large batches, this has nothing to do with the module or mySql but it seems that there is some trickery going on in node.

To format the data for an insert I need to have the data in a basic Array so I do something like this.

var keys = ['first_name', 'last_name', 'mi'], values, que, i, q;
values = []
que = []
i = 0
// data is a json array with corresponding keys

When i do this with one entry the app works wonderful, but when i do multiple i get the last entry many times.

There is nowhere that i overwrite que but for some reason after each iteration the all the values in que change. In normal javascript I use this method all the time to construct an array with reusable variable, then I store the instance in another location. Destroy the values in the reusable variable values.length = 0.

Does this method not work in node? because it seems that im storing the variable itself not the instance of the variable.

Problem courtesy of: Jacob Lowe


Seems like that's your problem, that its storing the variable itself.

To overcome this, you should do

values = [];

in your loop. That will create a new array for each iteration

Solution courtesy of: ControlAltDel


View additional discussion.

This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Node push Variables dynamic


Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription