Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
parov opened this issue Nov 7, 2016 · 2 comments
Closed

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

parov opened this issue Nov 7, 2016 · 2 comments

Comments

@parov
Copy link

parov commented Nov 7, 2016

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'
@wgrrrr
Copy link

wgrrrr commented Apr 5, 2017

Is there anything I can do to push this along? It appears this is a good PR and will fix this bug.

This particular bug is causing me no end of pain. Would love to see it merged and released. 🏁

@gaorlov
Copy link
Collaborator

gaorlov commented Mar 20, 2019

This looks resolved. Please reopen if not

@gaorlov gaorlov closed this as completed Mar 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants