# CouchDB river script arrays are weird

**URL:** <https://discuss.elastic.co/t/couchdb-river-script-arrays-are-weird/12080>\
**Category:** Elasticsearch\
**Created:** [May 22, 2013, 7:43pm UTC](https://discuss.elastic.co/t/couchdb-river-script-arrays-are-weird/12080 "2013-05-22T19:43:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ryan\_2](https://avatars.discourse-cdn.com/v4/letter/r/6bbea6/32.png) [@ryan\_2](https://discuss.elastic.co/u/ryan_2)\
**Post date:** [May 22, 2013, 7:43pm UTC](https://discuss.elastic.co/t/couchdb-river-script-arrays-are-weird/12080/1 "2013-05-22T19:43:17Z")

</div>

Hi there. Thanks for an amazing product...

We're currently having some issues with scripts in a couchdb river. We have  
most of our mutations all working, but there is a case where the incoming  
doc has a property that is an array of strings. No matter what we've tried,  
we simply cannot iterate this array to mutate the indexed document based in  
its values.

For example:

"script": "ctx.doc.myArray.forEach(function(obj) { /\* do something here \*/  
})"

results in the following error in the ES logs:  
org.mozilla.javascript.EcmaError: TypeError: Cannot call property forEach  
in object null. It is not a function, it is "undefined". (Script12.js#1)  
at  
org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3687)  
at  
org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3665)  
at  
org.mozilla.javascript.ScriptRuntime.typeError(ScriptRuntime.java:3693)  
at  
org.mozilla.javascript.ScriptRuntime.typeError3(ScriptRuntime.java:3719)  
at  
org.mozilla.javascript.ScriptRuntime.notFunctionError(ScriptRuntime.java:3786)  
at  
org.mozilla.javascript.ScriptRuntime.getPropFunctionAndThisHelper(ScriptRuntime.java:2269)  
at  
org.mozilla.javascript.ScriptRuntime.getPropFunctionAndThis(ScriptRuntime.java:2251)  
at  
org.mozilla.javascript.gen.Script12\_js\_11.\_c\_script\_0(Script12.js:1)  
at org.mozilla.javascript.gen.Script12\_js\_11.call(Script12.js)  
at  
org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:394)  
at  
org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3091)  
at org.mozilla.javascript.gen.Script12\_js\_11.call(Script12.js)  
at org.mozilla.javascript.gen.Script12\_js\_11.exec(Script12.js)  
at  
org.elasticsearch.script.javascript.JavaScriptScriptEngineService$JavaScriptExecutableScript.run(JavaScriptScriptEngineService.java:186)  
at  
org.elasticsearch.river.couchdb.CouchdbRiver.processLine(CouchdbRiver.java:241)  
at  
org.elasticsearch.river.couchdb.CouchdbRiver.access$500(CouchdbRiver.java:64)  
at  
org.elasticsearch.river.couchdb.CouchdbRiver$Indexer.run(CouchdbRiver.java:334)  
at java.lang.Thread.run(Thread.java:722)

That was pseudocode, because our script is doing more of course, but know  
that we _are_ sanity checking before that line (e.g. "if (ctx.doc.myArray)  
{}"), so the sanity checks pass (i.e. myArray is NOT null), but we still  
cannot iterate the darn thing.

Also weird with this is if we change our sanity checks to be more explicit  
(i.e. "if (typeof ctx.doc.myArray != 'undefined') {}"), we then get another  
odd error:

org.mozilla.javascript.EvaluatorException: Invalid JavaScript value of type  
java.util.ArrayList (Script13.js#1)  
at  
org.mozilla.javascript.DefaultErrorReporter.runtimeError(DefaultErrorReporter.java:77)  
at  
org.mozilla.javascript.Context.reportRuntimeError(Context.java:913)  
at  
org.mozilla.javascript.Context.reportRuntimeError(Context.java:969)  
at  
org.mozilla.javascript.Context.reportRuntimeError1(Context.java:932)  
at  
org.mozilla.javascript.ScriptRuntime.errorWithClassName(ScriptRuntime.java:3964)  
at  
org.mozilla.javascript.ScriptRuntime.typeof(ScriptRuntime.java:2530)  
at  
org.mozilla.javascript.gen.Script13\_js\_12.\_c\_script\_0(Script13.js:1)  
at org.mozilla.javascript.gen.Script13\_js\_12.call(Script13.js)  
at  
org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:394)  
at  
org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3091)  
at org.mozilla.javascript.gen.Script13\_js\_12.call(Script13.js)  
at org.mozilla.javascript.gen.Script13\_js\_12.exec(Script13.js)  
at  
org.elasticsearch.script.javascript.JavaScriptScriptEngineService$JavaScriptExecutableScript.run(JavaScriptScriptEngineService.java:186)  
at  
org.elasticsearch.river.couchdb.CouchdbRiver.processLine(CouchdbRiver.java:241)  
at  
org.elasticsearch.river.couchdb.CouchdbRiver.access$500(CouchdbRiver.java:64)  
at  
org.elasticsearch.river.couchdb.CouchdbRiver$Indexer.run(CouchdbRiver.java:334)  
at java.lang.Thread.run(Thread.java:722)

We have also tried testing general array iteration with tests like "['foo',  
'bar'].forEach(function(obj) {}" and that works fine. So there's something  
buggy about the incoming arrays on ctx.doc that is completely preventing us  
from iterating them. We've tried everything we can think of at this point,  
including using a regular old for..loop, assigning a new variable from the  
ctx.doc.myArray reference and iterating that, etc... nothing seems to work.

Any ideas?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![ryan\_2](https://avatars.discourse-cdn.com/v4/letter/r/6bbea6/32.png) [@ryan\_2](https://discuss.elastic.co/u/ryan_2)\
**Post date:** [May 28, 2013, 2:43pm UTC](https://discuss.elastic.co/t/couchdb-river-script-arrays-are-weird/12080/2 "2013-05-28T14:43:43Z")

</div>

Edited subject of post to be more clear. Bump.

Hi there. Thanks for an amazing product...

> We're currently having some issues with scripts in a couchdb river. We  
> have most of our mutations all working, but there is a case where the  
> incoming doc has a property that is an array of strings. No matter what  
> we've tried, we simply cannot iterate this array to mutate the indexed  
> document based in its values.
> 
> For example:
> 
> "script": "ctx.doc.myArray.forEach(function(obj) { /\* do something here  
> \*/ })"
> 
> results in the following error in the ES logs:  
> org.mozilla.javascript.EcmaError: TypeError: Cannot call property forEach  
> in object null. It is not a function, it is "undefined". (Script12.js#1)  
> at  
> org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3687)  
> at  
> org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3665)  
> at  
> org.mozilla.javascript.ScriptRuntime.typeError(ScriptRuntime.java:3693)  
> at  
> org.mozilla.javascript.ScriptRuntime.typeError3(ScriptRuntime.java:3719)  
> at  
> org.mozilla.javascript.ScriptRuntime.notFunctionError(ScriptRuntime.java:3786)  
> at  
> org.mozilla.javascript.ScriptRuntime.getPropFunctionAndThisHelper(ScriptRuntime.java:2269)  
> at  
> org.mozilla.javascript.ScriptRuntime.getPropFunctionAndThis(ScriptRuntime.java:2251)  
> at  
> org.mozilla.javascript.gen.Script12\_js\_11.\_c\_script\_0(Script12.js:1)  
> at org.mozilla.javascript.gen.Script12\_js\_11.call(Script12.js)  
> at  
> org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:394)  
> at  
> org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3091)  
> at org.mozilla.javascript.gen.Script12\_js\_11.call(Script12.js)  
> at org.mozilla.javascript.gen.Script12\_js\_11.exec(Script12.js)  
> at  
> org.elasticsearch.script.javascript.JavaScriptScriptEngineService$JavaScriptExecutableScript.run(JavaScriptScriptEngineService.java:186)  
> at  
> org.elasticsearch.river.couchdb.CouchdbRiver.processLine(CouchdbRiver.java:241)  
> at  
> org.elasticsearch.river.couchdb.CouchdbRiver.access$500(CouchdbRiver.java:64)  
> at  
> org.elasticsearch.river.couchdb.CouchdbRiver$Indexer.run(CouchdbRiver.java:334)  
> at java.lang.Thread.run(Thread.java:722)
> 
> That was pseudocode, because our script is doing more of course, but know  
> that we _are_ sanity checking before that line (e.g. "if (ctx.doc.myArray)  
> {}"), so the sanity checks pass (i.e. myArray is NOT null), but we still  
> cannot iterate the darn thing.
> 
> Also weird with this is if we change our sanity checks to be more explicit  
> (i.e. "if (typeof ctx.doc.myArray != 'undefined') {}"), we then get another  
> odd error:
> 
> org.mozilla.javascript.EvaluatorException: Invalid JavaScript value of  
> type java.util.ArrayList (Script13.js#1)  
> at  
> org.mozilla.javascript.DefaultErrorReporter.runtimeError(DefaultErrorReporter.java:77)  
> at  
> org.mozilla.javascript.Context.reportRuntimeError(Context.java:913)  
> at  
> org.mozilla.javascript.Context.reportRuntimeError(Context.java:969)  
> at  
> org.mozilla.javascript.Context.reportRuntimeError1(Context.java:932)  
> at  
> org.mozilla.javascript.ScriptRuntime.errorWithClassName(ScriptRuntime.java:3964)  
> at  
> org.mozilla.javascript.ScriptRuntime.typeof(ScriptRuntime.java:2530)  
> at  
> org.mozilla.javascript.gen.Script13\_js\_12.\_c\_script\_0(Script13.js:1)  
> at org.mozilla.javascript.gen.Script13\_js\_12.call(Script13.js)  
> at  
> org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:394)  
> at  
> org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3091)  
> at org.mozilla.javascript.gen.Script13\_js\_12.call(Script13.js)  
> at org.mozilla.javascript.gen.Script13\_js\_12.exec(Script13.js)  
> at  
> org.elasticsearch.script.javascript.JavaScriptScriptEngineService$JavaScriptExecutableScript.run(JavaScriptScriptEngineService.java:186)  
> at  
> org.elasticsearch.river.couchdb.CouchdbRiver.processLine(CouchdbRiver.java:241)  
> at  
> org.elasticsearch.river.couchdb.CouchdbRiver.access$500(CouchdbRiver.java:64)  
> at  
> org.elasticsearch.river.couchdb.CouchdbRiver$Indexer.run(CouchdbRiver.java:334)  
> at java.lang.Thread.run(Thread.java:722)
> 
> We have also tried testing general array iteration with tests like  
> "['foo', 'bar'].forEach(function(obj) {}" and that works fine. So there's  
> something buggy about the incoming arrays on ctx.doc that is completely  
> preventing us from iterating them. We've tried everything we can think of  
> at this point, including using a regular old for..loop, assigning a new  
> variable from the ctx.doc.myArray reference and iterating that, etc...  
> nothing seems to work.
> 
> Any ideas?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 2:34am UTC](https://discuss.elastic.co/t/couchdb-river-script-arrays-are-weird/12080/3 "2017-07-06T02:34:21Z")

</div>

Closed on July 6, 2017, 2:34am UTC


