#Hash>的未定义方法`bytesize'

tkn*_*knv 21 ruby google-app-engine jruby sinatra

我想将博客标签存储和更新到GAE中的数据存储区.当我运行该代码时,我收到此错误:

javax.servlet.ServletContext log: Application Error
/base/data/home/apps/yet-another-problem/1.334886515480009498/WEB-INF/gems/gems/sinatra-0.9.2/lib/sinatra/base.rb:45:in `each': undefined method `bytesize' for #<Hash:0x86684c> (NoMethodError)
Run Code Online (Sandbox Code Playgroud)

代码

class Labels
   class LabelData
    include Bumble
    ds :blog_element_labels
   end

  def update
    response = URLFetch.get($label_url)
    result = response.to_s
    result_headless = result.gsub("listLabels(",'')
    pure_result = result_headless.gsub(");",'')
    json_to_yaml = YAML::load(pure_result)['entry']['category']

    json_to_yaml.each do |label|
    @label = LabelData.find(:blog_element_labels => label['term'])
    @label = LabelData.create(:blog_element_labels => label['term']) if @label.nil?
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

并由cron job运行'/ job'

get '/job' do
  @labels = Labels.new
  @labels.update
end
Run Code Online (Sandbox Code Playgroud)

问题出在哪儿?请教我.

但是当第一次运行cron作业时,标签数据被存储,甚至发生错误.无法更新数据.

Luk*_*ins 42

我认为你有同样的问题在这里讨论过:当我在datamapper中尝试"all"方法时会发生错误

在您的情况下,Sinatra尝试获取@ lavels.update的返回值并将其转换为字符串以显示给用户.

试试这个,看看它是否解决了这个问题:

get '/job' do
  @labels = Labels.new
  @labels.update
  "Labels Updated"
end
Run Code Online (Sandbox Code Playgroud)

您的返回值现在是一个字符串,因此您不应该收到错误.