nov / fb_graph

This gem doesn't support FB Graph API v2.0+. Please use fb_graph2 gem instead.
MIT License
1.04k stars 191 forks source link

Error in lib/fb_graph/post.rb regarding message_tags. "undefined method `each' for nil:NilClass" #381

Closed arronmabrey closed 9 years ago

arronmabrey commented 10 years ago

Seems like Facebook is sending new data that fb_graph is unable to handle.


The following code...

identifier = '5xxxxxxxx_xxxxxxxx8'
access_token = 'CAAE...xxx...4gtEZD'

FbGraph::Post.new(identifier, access_token: access_token).fetch

...causes this code in lib/fb_graph/post.rb#L44-L50...

44 |  if (message_tags = attributes[:message_tags])
45 |    message_tags.each do |index, message_tag|
46 |      message_tag.each do |_message_tag_|
47 |        @message_tags << TaggedObject.new(_message_tag_[:id], _message_tag_)
48 |      end
49 |    end
50 |  end

...to raise this error.

NoMethodError: undefined method `each' for nil:NilClass
from /shared/bundle/gems/fb_graph-2.7.17/lib/fb_graph/post.rb:46:in `block in initialize'

Adding debug:

44 |  if (message_tags = attributes[:message_tags])
 * |    puts "message_tags:\n\t#{message_tags.inspect}"
45 |    message_tags.each do |index, message_tag|
 * |      puts "index:\n\t#{index.inspect}"
 * |      puts "message_tag:\n\t#{message_tag.inspect}"
46 |      message_tag.each do |_message_tag_|
47 |        @message_tags << TaggedObject.new(_message_tag_[:id], _message_tag_)
48 |      end
49 |    end
50 |  end

Outputs:

message_tags:
    [{"id"=>"5xxxxx2", "name"=>"John Smith", "type"=>"user", "offset"=>0, "length"=>14}]
index:
    {"id"=>"5xxxxx2", "name"=>"John Smith", "type"=>"user", "offset"=>0, "length"=>14}
message_tag:
    nil

NoMethodError: undefined method `each' for nil:NilClass
from /shared/bundle/gems/fb_graph-2.7.17/lib/fb_graph/post.rb:49:in `block in initialize'

Let me know if you need additional information. I'm happy to help.

Thanks, -- Arron

EDIT: we're using the latest version fb_graph-2.7.17
nov commented 10 years ago

Hum, FB returned wrong format of message tags. There are no changes in message tags format during v1.0 to v2.2. https://developers.facebook.com/docs/graph-api/reference/v2.2/post

Probably asking to FB developer support is better.

arronmabrey commented 10 years ago

The FB docs seem to suggest the data that was received is correct. Unless I'm missing something, which happens most of the time :smile:

data received

[
  { 
    "id" => "5xxxxx2",
    "name" => "John Smith",
    "type" => "user",
    "offset" => 0,
    "length" => 14
  }
]

Here is a pull-request that works correctly with the data FB is sending. https://github.com/nov/fb_graph/pull/382

nov commented 10 years ago

No, message_tags on posts should be a JSON object (Hash in ruby), not JSON array. ref.) https://github.com/nov/fb_graph-mock/blob/master/mock_json/posts/with_message.json

You might be fetching another type of object. Try fetch the object using Graph API Explorer with "metadata=true" option, then you'll see "type". https://developers.facebook.com/tools/explorer

arronmabrey commented 9 years ago

@nov thanks for the "metadata=true" tip. It looks like the object is a "comment" not a post. Sorry for the trouble. :-)