简体   繁体   中英

Why a Javascript array passed as function parameter may lose it's content when the function is called sequentially?

I'm sitting here for a while now wondering why I'm losing an array parameter on a function call when calling it a second time.

The script I'm working on is mapped after CouchDB/PouchDB and stores items as JSON strings in multiple storages (including local storage). Parameters are:

_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'm currently trying a PUT operation which by default updates an existing document. As I'm working with multiple storages, I also have a PUT SYNC , which "copy&pastes" versions of a document from one storage to another (with the goal having every version available on every storage). I'm also keeping a separate file with a document tree, which stores all the version hashes. This tree file is updated on SYNCs using the _revs_info supplied with the PUT .

My problem is sequential SYNC PUTs . The first one works, on the second I'm losing the _revs_info parameter. And I don't know why...

Here is my first call (from my QUnit module), which 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 fails, because the _revs_info array does not include anything. All other parameters and all random parameters I'm adding are transferred. If I add a string or object instead of an array they also safely make it into my script alive.

Array however... does not pass...

Question:
I have been sitting on this for a few hours trying to nail down points I have not found, but I'm pretty clueless. So does anyone know of reasons, why arrays might lose their content, when passing them on as parameters in Javascript?

Thanks!

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

It's completely possible for a JavaScript function to mutate an array passed in. Consider this example:

function removeAll(a) { a.splice(0); }

var arr = [1, 2, 3];
removeAll(arr);
console.log(arr); // empty array

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM