2012-11-21 158 views
8

所以我在玩弄mongo的東西。創建了一個新的heroku應用程序,添加了一個mongolab選項,但是每個mongoid方法都失敗了。Mongoid在紅寶石上失敗1.9.3

我搜索了一下,看起來這個問題在1.9.3之前是紅寶石常見的,但我運行的是1.9.3。

這裏是最簡單的失敗樣本:

require 'sinatra' 
require 'mongoid' 
require 'json' 
require "sinatra/reloader" if development? 

Mongoid.load!("mongoid.yml") 

class Company 
    include Mongoid::Document 

    field :code, type: String 
    field :sector, type: String 
    field :share_id, type: Integer 
    field :jse_code, type: Integer 
end 

get '/' do 
    "Hello" 
end 

get '/company' do 
    Company.exists? 
end 

閹我運行這個地方(對mongolab DB)或在Heroku上,我得到同樣的錯誤:

12:22:33 web.1 | NoMethodError - undefined method `match' for nil:NilClass: 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/mongoid-3.0.13/lib/mongoid/sessions/mongo_uri.rb:49:in `initialize' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/mongoid-3.0.13/lib/mongoid/sessions/factory.rb:103:in `new' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/mongoid-3.0.13/lib/mongoid/sessions/factory.rb:103:in `parse' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/mongoid-3.0.13/lib/mongoid/sessions/factory.rb:62:in `create_session' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/mongoid-3.0.13/lib/mongoid/sessions/factory.rb:43:in `default' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/mongoid-3.0.13/lib/mongoid/sessions.rb:109:in `default' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/mongoid-3.0.13/lib/mongoid/sessions.rb:378:in `__session__' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/mongoid-3.0.13/lib/mongoid/sessions.rb:213:in `mongo_session' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/mongoid-3.0.13/lib/mongoid/sessions.rb:157:in `collection' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/mongoid-3.0.13/lib/mongoid/contextual/mongo.rb:216:in `initialize' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/mongoid-3.0.13/lib/mongoid/contextual.rb:47:in `new' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/mongoid-3.0.13/lib/mongoid/contextual.rb:47:in `create_context' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/mongoid-3.0.13/lib/mongoid/contextual.rb:30:in `context' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/mongoid-3.0.13/lib/mongoid/contextual.rb:20:in `rescue in exists?' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/mongoid-3.0.13/lib/mongoid/contextual.rb:18:in `exists?' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/mongoid-3.0.13/lib/mongoid/finders.rb:43:in `exists?' 
12:22:33 web.1 | app.rb:22:in `block in <main>' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/sinatra-1.3.3/lib/sinatra/base.rb:1265:in `call' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/sinatra-1.3.3/lib/sinatra/base.rb:1265:in `block in compile!' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/sinatra-1.3.3/lib/sinatra/base.rb:835:in `[]' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/sinatra-1.3.3/lib/sinatra/base.rb:835:in `block (3 levels) in route!' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/sinatra-1.3.3/lib/sinatra/base.rb:851:in `route_eval' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/sinatra-1.3.3/lib/sinatra/base.rb:835:in `block (2 levels) in route!' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/sinatra-1.3.3/lib/sinatra/base.rb:872:in `block in process_route' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/sinatra-1.3.3/lib/sinatra/base.rb:870:in `catch' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/sinatra-1.3.3/lib/sinatra/base.rb:870:in `process_route' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/sinatra-1.3.3/lib/sinatra/base.rb:834:in `block in route!' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/sinatra-1.3.3/lib/sinatra/base.rb:833:in `each' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/sinatra-1.3.3/lib/sinatra/base.rb:833:in `route!' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/sinatra-1.3.3/lib/sinatra/base.rb:936:in `dispatch!' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/sinatra-1.3.3/lib/sinatra/base.rb:769:in `block in call!' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/sinatra-1.3.3/lib/sinatra/base.rb:921:in `block in invoke' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/sinatra-1.3.3/lib/sinatra/base.rb:921:in `catch' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/sinatra-1.3.3/lib/sinatra/base.rb:921:in `invoke' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/sinatra-1.3.3/lib/sinatra/base.rb:769:in `call!' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/sinatra-1.3.3/lib/sinatra/base.rb:755:in `call' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/rack-protection-1.2.0/lib/rack/protection/xss_header.rb:22:in `call' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/rack-protection-1.2.0/lib/rack/protection/path_traversal.rb:16:in `call' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/rack-protection-1.2.0/lib/rack/protection/json_csrf.rb:17:in `call' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/rack-protection-1.2.0/lib/rack/protection/base.rb:47:in `call' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/rack-protection-1.2.0/lib/rack/protection/xss_header.rb:22:in `call' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/rack-1.4.1/lib/rack/logger.rb:15:in `call' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/rack-1.4.1/lib/rack/commonlogger.rb:20:in `call' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/sinatra-1.3.3/lib/sinatra/base.rb:136:in `call' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/sinatra-1.3.3/lib/sinatra/base.rb:129:in `call' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/rack-1.4.1/lib/rack/head.rb:9:in `call' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/rack-1.4.1/lib/rack/methodoverride.rb:21:in `call' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/sinatra-1.3.3/lib/sinatra/showexceptions.rb:21:in `call' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/sinatra-1.3.3/lib/sinatra/base.rb:99:in `call' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/sinatra-1.3.3/lib/sinatra/base.rb:1389:in `block in call' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/sinatra-1.3.3/lib/sinatra/base.rb:1471:in `synchronize' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/sinatra-1.3.3/lib/sinatra/base.rb:1389:in `call' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/thin-1.5.0/lib/thin/connection.rb:81:in `block in pre_process' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/thin-1.5.0/lib/thin/connection.rb:79:in `catch' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/thin-1.5.0/lib/thin/connection.rb:79:in `pre_process' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/eventmachine-1.0.0/lib/eventmachine.rb:1037:in `call' 
12:22:33 web.1 | /Users/sdx/.rvm/gems/ruby-1.9.3-p327/gems/eventmachine-1.0.0/lib/eventmachine.rb:1037:in `block in spawn_threadpool' 
12:22:33 web.1 | 127.0.0.1 - - [21/Nov/2012 12:22:33] "GET /__sinatra__/500.png HTTP/1.1" 200 31056 0.0062 

我是什麼做錯了?

修訂這裏是我的mongoid.yml:

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

回答

9

基於堆棧跟蹤它看起來像構造爲mongo_uri被傳遞一個空值。我會檢查您mongoid.yml如下:

  • 它在正確的地方/在參數load!點到正確的位置
  • 它的正確配置
  • ,如果它是指任何環境變量(如MONGOLAB_URI對於MongoLab Heroku的插件),他們是在爲你的環境設置

相關的代碼行從mongo_uri.rb:49,在異常被拋出:

# Create the new uri from the provided string. 
    # 
    # @example Create the new uri. 
    # MongoUri.new(uri) 
    # 
    # @param [ String ] string The uri string. 
    # 
    # @since 3.0.0 
    def initialize(string) 
    @match = string.match(URI) 
    end 
+0

我更新了我的mongoid.yml文件的問題。我確信它讀取它會導致抱怨,例如,沒有默認值,或者如果env變量錯誤 –

+0

所以你是正確的,在本地它是一個連接問題。而在服務器上,儘管我的Gemfile說,它使用1.9.1。所以,2個不同的問題導致完美的混淆。 –