# Accessing an array in the \_source from a javascript script

**URL:** <https://discuss.elastic.co/t/accessing-an-array-in-the--source-from-a-javascript-script/25023>\
**Category:** Elasticsearch\
**Created:** [July 7, 2015, 9:54am UTC](https://discuss.elastic.co/t/accessing-an-array-in-the--source-from-a-javascript-script/25023 "2015-07-07T09:54:34Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jettro](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jettro/32/3418_2.png) [@jettro](https://discuss.elastic.co/u/jettro)\
**Post date:** [July 7, 2015, 9:54am UTC](https://discuss.elastic.co/t/accessing-an-array-in-the--source-from-a-javascript-script/25023/1 "2015-07-07T09:54:34Z")

</div>

Hi I am using a javascript based script in the config/script folder using the javascript plugin. Than I use an update request with a script based update. There is something strange happening when using a field containing an array. The following code fails with the message:

```
{
   "error": "ElasticsearchIllegalArgumentException[failed to execute script]; nested: EcmaError[TypeError: Cannot find function push in object [/logo.png]. (Script23.js#38)]; ",
   "status": 400
}

```

This is the script that gets executed:

```
var logger = org.elasticsearch.common.logging.ESLoggerFactory.getLogger('jsplugin');
logger.info(ctx._source.numPages);
var doc = ctx._source;

if (doc.numPages != null) {
    logger.info("EXISTING DOCUMENT");
    doc.numPages++;
} else {
    logger.info("NEW DOCUMENT");
    doc.numPages=1;
    doc.requests=[];
}

doc.requests.push(log.request);

```

I simplified the script, but the error is in the last line with the push. If it is a new document there is no error, but an existing document gives an error. The simplified request I do is below:

```
POST /logsbyip/unique/94.31.7.101/_update
{
  "script_file": "upsertlog",
  "lang": "js", 
  "scripted_upsert": true,
  "params": {
    "log": {
      "request": "/logo.png",
    }
  },
  "upsert": {}
}

```

There seems to be happening something with the \_source document that is provided to me in the script. Does anyone have an idea what I should do to add an item to the array?

---

<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, 12:03am UTC](https://discuss.elastic.co/t/accessing-an-array-in-the--source-from-a-javascript-script/25023/2 "2017-07-06T00:03:16Z")

</div>


