【问题标题】:Mongodb error code 10068 or 17287 with rails 4.1 and devise带有rails 4.1的Mongodb错误代码10068或17287并设计
【发布时间】:2014-04-15 19:00:38
【问题描述】:

我正在尝试将 mongoid 与 Rails 4.1.0 应用程序一起使用,但在 moongodb 2.6.0 上出现错误 17287(与早期版本的 mongodb 上的 10068 相同)。 这是错误消息:

The operation: #<Moped::Protocol::Query @length=127 @request_id=5 @response_to=0 @op_code=2004 @flags=[] @full_collection_name="educandose_development.users" @skip=0 @limit=-1 @selector={"$query"=>{"_id"=>{"$oid"=>BSON::ObjectId('534d6f4f6372618443000000')}}, "$orderby"=>{:_id=>1}} @fields=nil> failed with error 17287: "Can't canonicalize query: BadValue unknown operator: $oid" See https://github.com/mongodb/mongo/blob/master/docs/errors.md for details about this error.

知道什么地方出了问题吗?

【问题讨论】:

    标签: ruby-on-rails mongodb devise mongoid ruby-on-rails-4.1


    【解决方案1】:

    看了一会儿,我意识到 rails 4.1 上的新 json cookie 序列化程序会破坏对设计资源的 moped 查询。

    要解决此问题,请删除 cookies_serializer.rb 初始化程序中的以下行

    Rails.application.config.action_dispatch.cookies_serializer = :json
    

    您可能希望恢复旧的 session_store.rb 文件,其内容类似于:

    YourApp::Application.config.session_store :cookie_store, key: '_yourapp_session'
    

    或者试试devise的master分支。

    看这里:https://github.com/plataformatec/devise/issues/2949#issuecomment-40520236 在这里:https://github.com/plataformatec/devise/pull/2882

    【讨论】:

    • 如果您没有清除 cookie,则会导致:ActionView::Template::Error(不兼容的编组文件格式(无法读取)格式版本 4.8;给定 123.34)。清除您的 cookie,这就会消失。
    【解决方案2】:

    在 moped/session/json 格式修复之前,我暂时使用:

    # app/models/concerns/zero_oid_fix.rb
    module ZeroOidFix
      extend ActiveSupport::Concern
    
      module ClassMethods
        def serialize_from_session(key, salt)
          record = to_adapter.get((key[0]["$oid"] rescue nil))
          record if record && record.authenticatable_salt == salt
        end
      end
    end
    

    在设计模型中:

    class User
    
      devise :database_authenticatable, ...
    
      # NOTE: Has to be after devise
      include ZeroOidFix
    
      ...
    end
    

    希望这个答案很快就会过时。

    【讨论】:

    • 这个修复对我有用,谢谢。但是,我不太明白为什么会发生此错误以及此修复程序如何工作。你能解释一下发生了什么吗?
    • @Kira BSON ObjectId 序列化从将其存储为纯十六进制字符串更改为对象{ "$oid": "hexstring" },因此它更符合 MongoDB 的扩展 JSON 格式约定(在一个库中更改,但在另一个库中未更改)。
    • 这个修复对 mongoid 4.0.0 和 rails 4.1.4 来说就像一个魅力。
    • 为我工作!谢谢。
    • 如果您选中 remember me 复选框,则会中断。下次打开浏览器会报错。
    【解决方案3】:
    1. cookies_serializer.rb

      中注释掉下面的行

      Rails.application.config.action_dispatch.cookies_serializer = :json

    2. 删除 cookie。

    3. 重启服务器。

    在“rails 4.1.4、devise 3.2.4、mongoid 4.0.0”上为我工作

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多