Skip to content

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

Closed
@parov

Description

@parov

I have a response where relationships contains some data, but they're not included as a compound document.

{
  "data": {
    "id": "12345",
    "type": "books",
    "attributes": {
      "title": "A title",
    },
    "relationships": {
      "chapters": {
        "data": [
          {
            "id": "123456",
            "type": "chapters"
          }          
        ]
      }
    }
  }
}

when I try to access book.chapters I get NoMethodError: undefined method [] for nil:NilClass

https://github.com/chingor13/json_api_client/blob/master/lib/json_api_client/resource.rb#L445

#look in included data
if relationship_definitions.key?("data")
  return last_result_set.included.data_for(method, relationship_definitions)
end

This code checks for the relationships array relationship_definitions.key?("data"), and calls last_result_set.included that returns
#<JsonApiClient::IncludedData:0x0055e4e47cf398 @data={}> , which generates the exception above.

Why not checking the included data instead of the relationships only?

Here the backtrace:

json_api_client-1.3.0/lib/json_api_client/included_data.rb:43:in `record_for'
json_api_client-1.3.0/lib/json_api_client/included_data.rb:27:in `block in data_for'
json_api_client-1.3.0/lib/json_api_client/included_data.rb:26:in `map'
json_api_client-1.3.0/lib/json_api_client/included_data.rb:26:in `data_for'
json_api_client-1.3.0/lib/json_api_client/resource.rb:447:in `method_missing'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions