# NoMethodError: undefined method \`\[\]' for nil:NilClass

**URL:** <https://discuss.elastic.co/t/nomethoderror-undefined-method-for-nil-nilclass/180696>\
**Category:** Logstash\
**Created:** [May 12, 2019, 2:47pm UTC](https://discuss.elastic.co/t/nomethoderror-undefined-method-for-nil-nilclass/180696 "2019-05-12T14:47:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pirimoglu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pirimoglu/32/46045_2.png) [@pirimoglu](https://discuss.elastic.co/u/pirimoglu)\
**Post date:** [May 12, 2019, 2:47pm UTC](https://discuss.elastic.co/t/nomethoderror-undefined-method-for-nil-nilclass/180696/1 "2019-05-12T14:47:32Z")

</div>

Hi,

I try to use logstash.filters.aggregate plugin.

I get an aggregate exception.

Aggregate exception occurred  
{:error=\>#\<NoMethodError: undefined method `' for nil:NilClass\>, :code=\>"\n\t\t\tmap['user']['scopes'] \<\< {'id' =\> 'asd', 'name' =\> 'asd'}\n\t\t\tevent.cancel()\n\t\t", :map=\>{}, :event\_data=\>{"@timestamp"=\>2019-05-12T14:28:00.803Z, "user.scopes.id"=\>nil, "user.scopes.name"=\>"xxx", "@version"=\>"1", "id"=\>1000002, "type"=\>"yyy", "user"=\>{"tel2"=\>nil, "address"=\>nil, "relatedbapuser"=\>nil, "university"=\>"xxx", "titleId"=\>xxx, "departmentId"=\>xxx, "title"=\>"xxx", "isdeleted"=\>"false", "countryId"=\>xxx, "relatedid"=\>nil}}}

logstash.conf

```
aggregate {
	task_id => '%{id}'
	code => "
		map['user']['scopes'] << {'id' => 'asd', 'name' => 'asd'}
		event.cancel()
	"
	push_previous_map_as_event => true
	timeout => 3
}

```

Do you have any suggestion about this exception?

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [May 12, 2019, 3:49pm UTC](https://discuss.elastic.co/t/nomethoderror-undefined-method-for-nil-nilclass/180696/2 "2019-05-12T15:49:46Z")

</div>

Try adding

```
    map['user'] ||= {}
    map['user']['scopes'] ||= []
```

---

<div class="post-metadata">

**Author:** ![pirimoglu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pirimoglu/32/46045_2.png) [@pirimoglu](https://discuss.elastic.co/u/pirimoglu)\
**Post date:** [May 13, 2019, 3:39am UTC](https://discuss.elastic.co/t/nomethoderror-undefined-method-for-nil-nilclass/180696/3 "2019-05-13T03:39:35Z")

</div>

Thanks but not work ☹

In the code block changes not effected to output. But remove fields in remove\_field part works.

```
input {
	jdbc {
        jdbc_driver_library => "...\sqljdbc42.jar"
        jdbc_driver_class => "com.microsoft.sqlserver.jdbc.SQLServerDriver"
        jdbc_connection_string => "..."
        jdbc_user => "..."
        jdbc_password => "..."
        jdbc_validate_connection => true
        clean_run => true
        schedule => "..."
        type => "a_type"
        statement => "..."
    }
}
filter {
	if [type] == "a_type"{
		aggregate {
			task_id => '%{user.id}'
			code => "
				map['user'] ||= {}
				map['user']['id'] = event.get('user.id')
				map['user']['scopes'] ||= []
				map['user']['scopes'] << {'id' => event.get('user.scopes.id'), 'name' => event.get('user.scopes.name')}
			"
			remove_field => ["user.id", "user.scopes.id", "user.scopes.name"]
		}
    }
}
output {
	if [type] == "a_type"{
        elasticsearch {
			hosts => ["localhost:9200"]
			index => "xxx"
			document_type => "yyy"
			document_id => "%{id}"
        }
    }
}
```

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [May 13, 2019, 1:19pm UTC](https://discuss.elastic.co/t/nomethoderror-undefined-method-for-nil-nilclass/180696/4 "2019-05-13T13:19:04Z")

</div>

In your sample data you do not have a [user.id] field, just [id]

---

<div class="post-metadata">

**Author:** ![pirimoglu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pirimoglu/32/46045_2.png) [@pirimoglu](https://discuss.elastic.co/u/pirimoglu)\
**Post date:** [May 13, 2019, 3:38pm UTC](https://discuss.elastic.co/t/nomethoderror-undefined-method-for-nil-nilclass/180696/5 "2019-05-13T15:38:01Z")

</div>

Yes, you are right. I change some piece of config. But is it matter id or user.id field exist?

Some fields return from sql are below:  
id, user.id, user.name, user.surname, user.email, user.scopes.id, user.scopes.name

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [May 13, 2019, 4:13pm UTC](https://discuss.elastic.co/t/nomethoderror-undefined-method-for-nil-nilclass/180696/6 "2019-05-13T16:13:40Z")

</div>

You are right, that might prevent it working (if the task\_id field does not exist) but it would not result in that error. I would expect that error if you commented out

```
map['user'] ||= {}

```

I suggest running "--log.level debug --config.debug --config.test\_and\_exit" and see if the configuration is the one you expect (that you do not, for example, have both my.conf and my.conf.bck getting picked up).

---

<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:** [June 10, 2019, 4:17pm UTC](https://discuss.elastic.co/t/nomethoderror-undefined-method-for-nil-nilclass/180696/7 "2019-06-10T16:17:25Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
