我正在尝试使用来自同一 API 的信息为 2 个表播种。我填满了第一个表,但是尽管代码相似,但第二个表还是给了我错误



JSON数据如下。。。

{
"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",}

对于我保存基本比赛信息的表格,我用这个。。。

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 

它运行得很好,做了我想做的事。我需要填写我的另一张表,其中包括阵容数据,这些数据一直在返回我;nil的未定义方法[]:Nilclass";以及";TypeError:没有字符串到整数的隐式转换";错误。所以,为了测试,我删除了第二种方法,保留了第一种方法,现在第一种方法甚至不再有效,给了我"NoMethodError:nil:NilClass的未定义方法"fetch_value";尽管是与以前工作的代码完全相同的代码。

我已经进入postresql,确认两个表都存在,并且第一个表已经正确填充,但现在,这两个方法都被破坏了。

完整种子文件

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 

完整错误消息

$ 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)

我哪里错了?

此处为"lineup": { "data": [ { "team_id": 3645, "fixture_id": 18449920, "player_id": 393, "player_name": "Brad Guzan", "number": 1, "position": "G",}是在密钥"中包含数组的散列;数据";。请用数据运行一个循环并填充Lineup表。像

@match['data'].each do |lineup|
lineup['data'].each do |l|
Lineup.create(
display_name: l["player_name"],
number: l["number"],
position: l["position"],
)
end
end 

您的Lineup.create在此行末尾有一个额外的逗号:

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

哪里出了问题?

您没有仔细阅读错误消息,没有使用调试工具,没有在StackOverflow上共享完整错误消息,也没有向StackOverlow共享可复制的代码示例(您只共享了部分/不完整的JSON数据,我不知道这个fetch_value方法是在哪里定义/调用的(。

第一个错误:

undefined method [] for nil:Nilclass

指示某个nil。什么是nil?我不知道,因为你没有透露细节。读取错误消息的行号,它会告诉您哪个变量是nil

例如,可能(??(错误来自:match["time"]["starting_at"]["date"],因为match["time"]["starting_at"]nil?也许解决办法是用"startingAt"替换"starting_at"?!或者这个字段有时可能会丢失,所以您的代码需要优雅地处理它——例如match.dig("time", "starting_at", "date")

或者,也许@lineup实际上给了你一组完全不同的数据,比如{ "error": { "message": "Unauthenticated", "code": 403 } },所以解决方案是优雅地处理无效的响应状态?

如果没有更多信息,我只能推测可能的原因和解决方案。

您还可以使用像byebug这样的调试工具来帮助诊断变量,而不是依赖于重新运行代码和/或使用puts语句来查看发生了什么

相关内容

  • 没有找到相关文章

最新更新