GCRC / nunaliit

Nunaliit Atlas Framework
BSD 3-Clause "New" or "Revised" License
46 stars 15 forks source link

Appending value to nunaliit_layers array using the Data Modification tool #231

Closed pulsifer closed 10 years ago

pulsifer commented 10 years ago

I am attempting to add a new value to the nunaliit_layers array for documents in a Query list generated by the Data Modification tool. Using the Transfrom-> Javascript Replace function, I have attempted the following:

function(doc, onTransformedFn, onSkippedFn){ doc.nunaliit_layers[doc.nunaliit_layers.length] = "new_layer_name"; onTransformedFn(); }

function(doc, onTransformedFn, onSkippedFn){ doc.nunaliit_layers.push = "new_layer_name"; onTransformedFn(); }

Both result in an error of Cannot Set property of (length | push) of undefined. It seems that this should work. Any thoughts on whether this error is related to how the Data Modification tool processes this script or whether I am missing something more fundamental?

jpfiset commented 10 years ago

Looks like the array does not exist. How about:

function(doc, onTransformedFn, onSkippedFn){
    if( !doc.nunaliit_layers ){
        doc.nunaliit_layers = [];
    };
    doc.nunaliit_layers.push('new_layer_name');
    onTransformedFn();
}
pulsifer commented 10 years ago

All of the documents in the list have a nunaliit_layers property. In the tree view, the value(s) in this property are displayed as an array (i.e. in square brackets). I will try the fix above, however I am interested in why the array is not recognized. I will report back one way or the other.

ahayes commented 10 years ago

Well, the one thing I noticed is that you are trying to assign a value to push instead of invoking it with an argument. JP's code is simply more correct in that it checks first (which you should always do) before trying to add something.

Amos Hayes Geomatics and Cartographic Research Centre Carleton University, Canada http://gcrc.carleton.ca ahayes@gcrc.carleton.ca +1.613.520.2600x8179

On 2 July 2014 12:01, pulsifer notifications@github.com wrote:

All of the documents in the list have a nunaliit_layers property. In the tree view, the value(s) in this property are displayed as an array (i.e. in square brackets). I will try the fix above, however I am interested in why the array is not recognized. I will report back one way or the other.

— Reply to this email directly or view it on GitHub https://github.com/GCRC/nunaliit/issues/231#issuecomment-47795934.

pulsifer commented 10 years ago

Great. Thanks for the explanation. That did the job!

jpfiset commented 10 years ago

When the modification tool accepts a script, it first attempt to evaluate the script for correctness. If something is wrong with the script, it warns the user that something is wrong with the script. To do so, it runs the script once with a dummy document.

This is probably why you were running into a document that did not have a "nunaliit_layers" attribute.