Home > OS >  I'm trying to seed 2 tables with information from the same API. I filled the first table, but t
I'm trying to seed 2 tables with information from the same API. I filled the first table, but t

Time:03-15

The JSON data looks like this...

 {
    "data": [
            "scores": {
                "localteam_score": 3,
                "visitorteam_score": 1,
                      }
            "localTeam": {
                "data": {
                     "name": "Atlanta United",
                      "logo_path": "https://cdn.sportmonks.com/images/soccer/teams/3/323.png",
                }
            },
            "visitorTeam": {
                "data": {
                    "name": "Sporting KC",
                    "logo_path": "https://cdn.sportmonks.com/images/soccer/teams/3/323.png",
                }
            },
            "lineup": {
                "data": [
                    {
                        "team_id": 3645,
                        "fixture_id": 18449920,
                        "player_id": 393,
                        "player_name": "Brad Guzan",
                        "number": 1,
                        "position": "G",}

For the table where I keep basic match information, I seeded it with this...

    require 'rest-client'
rest_client = RestClient.get 'https://soccer.sportmonks.com/api/v2.0/fixtures/between/2022-01-01/2022-12-01/3645?api_token=__&include=localTeam,visitorTeam,goals,'
@match = JSON.parse(rest_client)


@match['data'].each do |match|
    Match.create(
        local_team: match["localTeam"]["data"]["name"],
        away_team: match["visitorTeam"]["data"]["name"],
        localteam_score: match["scores"]["localteam_score"],
        awayteam_score: match["scores"]["visitorteam_score"],
        awaylogo: match["visitorTeam"]["data"]["logo_path"],
        locallogo: match["localTeam"]["data"]["logo_path"])
    end 

Which worked just fine and did what I wanted it to do. I needed to fill my other table, consisting of lineup data, which has been returning me "undefined method [] for nil:Nilclass" and "TypeError: no implicit conversion of string into integer" errors. So, to test, I deleted the 2nd method and kept the first, and now the first one will not even work anymore and is giving me "NoMethodError: undefined method `fetch_value' for nil:NilClass" despite being the exact same code that was working before.

I have gone into postresql and confirmed that both tables exist and that the first one has been filled properly, but now, it's like both of those methods are busted.

Full seeds file

require 'json'
require 'dotenv'
require 'rest-client'
require 'open-uri'
Dotenv.load

require 'rest-client'
rest_client = RestClient.get 'https://soccer.sportmonks.com/api/v2.0/fixtures/between/2022-01-01/2022-12-01/3645?api_token=p5o0nolOT4JUo5lYXrBFY8Ess6vuRZJ9Rt5jmOx3DmJjgZpT9ghmfYbZHIdj&include=localTeam,visitorTeam,goals,'
@match = JSON.parse(rest_client)


@match['data'].each do |match|
    Match.create(
        local_team: match["localTeam"]["data"]["name"],
        away_team: match["visitorTeam"]["data"]["name"],
        localteam_score: match["scores"]["localteam_score"],
        awayteam_score: match["scores"]["visitorteam_score"],
        awaylogo: match["visitorTeam"]["data"]["logo_path"],
        locallogo: match["localTeam"]["data"]["logo_path"])
    end 
    
@match['data'].each do |lineup|
    Lineup.create(
        display_name: lineup["lineup"]["data"]["player_name"],
        number: lineup["lineup"]["data"]["number"],
        position: lineup["lineup"]["data"]["position"],
        )
    end 

Full error message

$ rails db:seed
rails aborted!
NoMethodError: undefined method `fetch_value' for nil:NilClass
/home/ubuntu/environment/rated_app/db/seeds.rb:20:in `block in <main>'
/home/ubuntu/environment/rated_app/db/seeds.rb:19:in `each'
/home/ubuntu/environment/rated_app/db/seeds.rb:19:in `<main>'
/home/ubuntu/environment/rated_app/bin/rails:5:in `<top (required)>'
/home/ubuntu/environment/rated_app/bin/spring:10:in `require'
/home/ubuntu/environment/rated_app/bin/spring:10:in `block in <top (required)>'
/home/ubuntu/environment/rated_app/bin/spring:7:in `tap'
/home/ubuntu/environment/rated_app/bin/spring:7:in `<top (required)>'

Caused by:
NoMethodError: undefined method `fetch_value' for nil:NilClass
/home/ubuntu/environment/rated_app/db/seeds.rb:20:in `block in <main>'
/home/ubuntu/environment/rated_app/db/seeds.rb:19:in `each'
/home/ubuntu/environment/rated_app/db/seeds.rb:19:in `<main>'
/home/ubuntu/environment/rated_app/bin/rails:5:in `<top (required)>'
/home/ubuntu/environment/rated_app/bin/spring:10:in `require'
/home/ubuntu/environment/rated_app/bin/spring:10:in `block in <top (required)>'
/home/ubuntu/environment/rated_app/bin/spring:7:in `tap'
/home/ubuntu/environment/rated_app/bin/spring:7:in `<top (required)>'
Tasks: TOP => db:seed
(See full trace by running task with --trace)

Where am I going wrong?

CodePudding user response:

Where am I going wrong?

You are not reading the error message carefully, you're not using debugging tools, you haven't shared the full error message here on StackOverflow, and you're not sharing a reproducible code sample to StackOverlow (you've only shared partial/incomplete JSON data, and I have no idea where this fetch_value method is being defined/invoked).

The first error:

undefined method [] for nil:Nilclass

Indicates that something is nil. What is nil? I don't know, because you didn't share the details. Read the line number of the error message, and it will tell you which variable is nil.

For example, maybe (??) the error is coming from: match["time"]["starting_at"]["date"], because match["time"]["starting_at"] is nil? Maybe the fix is to replace "starting_at" with "startingAt"?! Or maybe this field can sometimes be missing, so your code needs to handle that gracefully - e.g. match.dig("time", "starting_at", "date")?

Or, maybe @lineup is actually giving you a completely different set of data like { "error": { "message": "Unauthenticated", "code": 403 } }, and so the solution is to gracefully handle invalid response statuses?

Without more information, I can only speculate on the possible cause and solution.

You can also use a debugging tool like byebug to help diagnose variables, instead of rely on re-running the code and/or using puts statements to see what's going on.

CodePudding user response:

Your Lineup.create has an extra comma at the end of this line:

position: lineup["data"]["position"],
  • Related