Why can a Javascript array passed as a parameter to a function lose its contents when a function is called sequentially?

I have been sitting here for a while, wondering why I lose an array parameter when calling a function when calling it a second time.

The script I'm working on is displayed after CouchDB / PouchDB and stores the items as JSON strings in many repositories (including local storage). Options:

_id id of the item _rev revision string (version), counter and hash _content whatever content _revisions array of all prior hashes and current counter _revs_info all previous revisions of this item with status 

I am currently trying to perform a PUT operation, which by default updates an existing document. When I work with multiple repositories, I also have PUT SYNC , which โ€œcopies and pastesโ€ versions of a document from one repository to another (with the goal of having any version available for each repository). I also save a separate file with a document tree, which stores all version hashes. This tree file is updated on SYNCs using _revs_info supplied with PUT .

My problem is consistent SYNC PUTs . The first one works, and in the second I lose the _revs_info parameter. And I do not know why...

Here is my first call (from my QUnit module) that works fine:

 o.jio.put({ "content":'a_new_version', "_id":'myDoc', "_rev":"4-b5bb2f1657ac5ac270c14b2335e51ef1ffccc0a7259e14bce46380d6c446eb89", "_revs_info":[ {"rev":"4-b5bb2f1657ac5ac270c14b2335e51ef1ffccc0a7259e14bce46380d6c446eb89","status":"available"}, {"rev":"3-a9dac9ff5c8e1b2fce58e5397e9b6a8de729d5c6eff8f26a7b71df6348986123","status":"deleted"}, {"rev":fake_rev_1,"status":"deleted"}, {"rev":fake_rev_0,"status":"deleted"} ], "_revisions":{ "start":4, "ids":[ "b5bb2f1657ac5ac270c14b2335e51ef1ffccc0a7259e14bce46380d6c446eb89", "a9dac9ff5c8e1b2fce58e5397e9b6a8de729d5c6eff8f26a7b71df6348986123", fake_id_1, fake_id_0 ]} }, function(err, response) { // run tests }); 

However, when I call the same function a second time:

 o.jio.put({ "content":'a_deleted_version', "_id":'myDoc', "_rev":"3-05210795b6aa8cb5e1e7f021960d233cf963f1052b1a41777ca1a2aff8fd4b61", "_revs_info":[ {"rev":"3-05210795b6aa8cb5e1e7f021960d233cf963f1052b1a41777ca1a2aff8fd4b61","status":"deleted"},{"rev":"2-67ac10df5b7e2582f2ea2344b01c68d461f44b98fef2c5cba5073cc3bdb5a844","status":"deleted"},{"rev":fake_rev_2,"status":"deleted"}], "_revisions":{ "start":3, "ids":[ "05210795b6aa8cb5e1e7f021960d233cf963f1052b1a41777ca1a2aff8fd4b61", "67ac10df5b7e2582f2ea2344b01c68d461f44b98fef2c5cba5073cc3bdb5a844", fake_id_2 ]} }, function(err, response) { // run tests }); 

My script does not work because the _revs_info array _revs_info not contain anything. All other parameters and all random parameters that I add are carried over. If I add string or object instead of array , they will also safely turn it into my script live.

However, the array ... does not pass ...

Question:
I have been sitting on this for several hours, trying to nail out points that I did not find, but I am rather ignorant. So does anyone know the reasons why arrays can lose its content by passing them as parameters in Javascript?

Thanks!

EDIT :
I added a regular PUT after my first SYNC-PUT , which went fine (without defining _revs_info ).

+4
source share
1 answer

It is entirely possible for the JavaScript function to modify the passed array. Consider this example:

 function removeAll(a) { a.splice(0); } var arr = [1, 2, 3]; removeAll(arr); console.log(arr); // empty array 
0
source

All Articles