【问题标题】:Heroku nil class error while saving an object保存对象时Heroku nil类错误
【发布时间】:2013-06-25 20:27:24
【问题描述】:

我在 heroku 上有一个使用 mongoid 和 delay_job 的应用程序,我已将 ruby​​ 版本从 1.9.2 更改为 1.9.3 我收到错误

undefined method `match' for nil:NilClass

当我尝试将对象保存到 mongoid 数据库时,我的 mongoid.yaml 文件看起来像

production:
  sessions:
    default:
      uri: <%= ENV['MONGOHQ_URL'] %>
      options:
        skip_version_check: true
        safe: true

痕迹在这里

NoMethodError: undefined method `match' for nil:NilClass
from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.13/lib/mongoid/sessions/mongo_uri.rb:49:in `initialize'
from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.13/lib/mongoid/sessions/factory.rb:103:in `new'
from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.13/lib/mongoid/sessions/factory.rb:103:in `parse'
from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.13/lib/mongoid/sessions/factory.rb:62:in `create_session'
from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.13/lib/mongoid/sessions/factory.rb:43:in `default'
from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.13/lib/mongoid/sessions.rb:109:in `default'
from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.13/lib/mongoid/sessions.rb:378:in `__session__'
from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.13/lib/mongoid/sessions.rb:213:in `mongo_session'
from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.13/lib/mongoid/sessions.rb:157:in `collection'
from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.13/lib/mongoid/sessions.rb:25:in `collection'
from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.13/lib/mongoid/persistence/operations.rb:27:in `collection'
from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.13/lib/mongoid/persistence/operations/insert.rb:27:in `block in persist'
from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.13/lib/mongoid/persistence/insertion.rb:25:in `block (2 levels) in prepare'
from /app/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.6/lib/active_support/callbacks.rb:403:in `_run__4034630009416245289__create__685777988298500290__callbacks'
from /app/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.6/lib/active_support/callbacks.rb:405:in `__run_callback'
from /app/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.6/lib/active_support/callbacks.rb:385:in `_run_create_callbacks'

... 2 levels...
from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.13/lib/mongoid/persistence/insertion.rb:24:in `block in prepare'
from /app/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.6/lib/active_support/callbacks.rb:403:in `_run__4034630009416245289__save__685777988298500290__callbacks'
from /app/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.6/lib/active_support/callbacks.rb:405:in `__run_callback'
from /app/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.6/lib/active_support/callbacks.rb:385:in `_run_save_callbacks'
from /app/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.6/lib/active_support/callbacks.rb:81:in `run_callbacks'
from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.13/lib/mongoid/callbacks.rb:114:in `run_callbacks'
from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.13/lib/mongoid/persistence/insertion.rb:23:in `prepare'
from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.13/lib/mongoid/persistence/operations/insert.rb:26:in `persist'
from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.13/lib/mongoid/persistence.rb:50:in `insert'
from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.13/lib/mongoid/persistence.rb:79:in `save'
from (irb):2
from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.6/lib/rails/commands/console.rb:47:in `start'
from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.6/lib/rails/commands/console.rb:8:in `start'
from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.6/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'

任何人都可以帮助我吗?提前致谢

【问题讨论】:

  • 什么是回溯报告?此错误发生在哪个文件中?
  • 我已经用回溯更新了问题
  • 嗯...根据您的回溯,无论出于何种原因,Heroku 似乎认为您使用的是 Ruby 1.9.1。任何想法为什么会这样?这个答案可能会有所帮助:stackoverflow.com/questions/11943470/…
  • 我已经检查了 heroku run ruby​​ -v 它返回的 ruby​​ 1.9.3p448

标签: ruby-on-rails heroku mongoid delayed-job


【解决方案1】:

发生错误是因为此方法https://github.com/mongoid/mongoid/blob/v3.0.13/lib/mongoid/sessions/mongo_uri.rb#L49 的输入参数为零。

进一步备份调用堆栈以找出该字符串的来源(请参阅https://github.com/mongoid/mongoid/blob/v3.0.13/lib/mongoid/sessions/factory.rb#L103),看起来您的uri 配置参数为零。 uri 键存在但为空,因此您能否通过运行 heroku config 检查您的 MONGOHQ_URL 配置参数。这应该由 MongoHQ 插件设置。

堆栈跟踪中的 Ruby 1.9.1 路径无需担心。见Why Do Heroku Stack Traces indicate Ruby 1.9.1 is in use?

【讨论】:

  • 这应该在您添加 MongoHQ 插件时自动配置。您可以使用heroku addons 检查插件是否仍附加到您的应用程序,以及是否需要使用heroku config:set KEY=VALUE 手动设置配置参数。您必须通过 Heroku 网站上您应用的插件页面为您的插件找到正确的 URL。
  • 这个插件可以免费使用吗?
猜你喜欢
  • 2014-08-15
  • 2014-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多