0

I'm including what I think are the important bits of code below...

updates = {};
products = {};
newProducts = category.items[0].items;
oldProducts = categoryData[i][0].items;
itemsObj = {};    
updates['changes'] = [];

Followed by a bunch of code that adds items to 'changes' such as...

itemsObj['update'] = "modify";
itemsObj['id'] = oldProduct.id;
itemsObj['type'] = oldProduct.type;
itemsObj['qty'] = newProduct.qty;
updates['changes'].push(itemsObj);

After all the updates, I called an alert below during testing. When I later attempted to remove it I found I no longer got the 'changes' above.

alert(JSON.stringify(updates));

If I don't do the alert above then 'updates' (ultimately updateJSON) only returns 'productRequest' below, not 'changes' above. I've tried setting the stringified version of updates to another variable, logging to console, and some other hacky ways I found online but only the above alert works for me. Obviously there's a way to fix this and I can't have an alert firing in production.

products['action'] =  "editProducts";
products['id'] =  xxxxxxxx;

updates['productRequest'] = products;

updateFinal = JSON.stringify(updates);
consoleLog(updateFinal);

Anyone have what I expect is an easy solution?

DSel
  • 9
  • 4
  • 1
    You really have not posted enough code. – Pointy Jun 24 '13 at 19:53
  • There must be something weird going on. There's no way, that JSON.Stringify would return a different result depending on whether it's assigned to a variable or alerted in a box. What happend if you assign it first and alert the variable? I suspect it's just a stupid little error you overlooked. (I hate those. ;)) – GolezTrol Jun 24 '13 at 19:57
  • 1
    I can almost guarantee you're making an AJAX call in there somewhere in the code you have not shown, and you are expecting execution to continue normally with the AJAX result instead of using a callback to finish your processing? – Dark Falcon Jun 24 '13 at 19:57
  • I agree with @DarkFalcon in which case you should read this http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call . If this is the case, alerting anything at all will work and it has nothing to do with stringify. – James Montagne Jun 24 '13 at 20:02
  • What goes into JSON comes out of JSON. So there is an issue somewhere else. – Steven Jun 24 '13 at 20:15
  • Dark Falcon and James Montagne, you are right on and thanks for the link. That's exactly what I need - callback function. The alert above is just after the ajax call retrieving the data to modify as is the code that follows. "Updates" is build up inside that ajax call. – DSel Jun 24 '13 at 20:16

0 Answers0