【问题标题】:Mongoid::Errors::DocumentNotFound raise_not_found_errorMongoid::Errors::DocumentNotFound raise_not_found_error
【发布时间】:2013-09-09 10:20:07
【问题描述】:

* 在底部更新 *

在寻找不存在的用户时,我得到:

Mongoid::Errors::DocumentNotFound in UsersController#show

问题: 未找到 ID 为 22 的类用户的文档。摘要:何时 使用 id 或 id 数组调用 User.find,每个参数必须 匹配数据库中的文档,否则将引发此错误。这 搜索的 id(s): 22 ...(总共 1 个)和以下 id 未找到:22. 解决方法:搜索数据库中的 id 或 将 Mongoid.raise_not_found_error 配置选项设置为 false, 这将导致返回 nil 而不是引发此错误 搜索单个 id 时,或仅搜索匹配的文档时 搜索倍数。

但是我将 raise_not_found_error 设置为 false

mongoid.yml

development:
  adapter: 'mongoid'
  # Configure available database sessions. (required)
  sessions:
    # Defines the default session. (required)
    default:
      # Defines the name of the default database that Mongoid can connect to.
      # (required).
      database: blog_development
      # Provides the hosts the default session can connect to. Must be an array
      # of host:port pairs. (required)
      hosts:
        - localhost:27017
      options:
          allow_dynamic_fields: false
          identity_map_enabled: true
          include_root_in_json: true
          include_type_for_serialization: true
          # Note this can also be true if you want to preload everything, but this is
          # almost never necessary. Most of the time set this to false.
          preload_models:
            - Canvas
            - Browser
            - Firefox
          scope_overwrite_exception: true
          raise_not_found_error: false
          skip_version_check: false
          use_activesupport_time_zone: false
          use_utc: true
  # Configure Mongoid specific options. (optional)
  options:
    # Enable the identity map, needed for eager loading. (default: false)
    # identity_map_enabled: false

    # Includes the root model name in json serialization. (default: false)
    # include_root_in_json: false

    # Include the _type field in serializaion. (default: false)
    # include_type_for_serialization: false

    # Preload all models in development, needed when models use
    # inheritance. (default: false)
    # preload_models: false

    # Protect id and type from mass assignment. (default: true)
    # protect_sensitive_fields: true

    # Raise an error when performing a #find and the document is not found.
    # (default: true)
    raise_not_found_error: false

    # Raise an error when defining a scope with the same name as an
    # existing method. (default: false)
    scope_overwrite_exception: false

    # Skip the database version check, used when connecting to a db without
    # admin access. (default: false)
    # skip_version_check: false

    # Use Active Support's time zone in conversions. (default: true)
    # use_activesupport_time_zone: true

    # Ensure all times are UTC in the app side. (default: false)
    # use_utc: false
test:
  sessions:
    default:
      database: blog_test
      hosts:
        - localhost:27017
      options:
        consistency: :strong
        # In the test environment we lower the retries and retry interval to
        # low amounts for fast failures.
        max_retries: 1
        retry_interval: 0

控制器

  # GET /users/1
  # GET /users/1.json
  def show

    @user = User.find(params[:id])

    render json: @user
  end

* 更新 ** 通过执行以下操作修复了一个空响应(不是 json 格式):

  def show

    @user = User.find(params[:id])
    if @user.nil?
      @user = []
    end

    render json: @user
  end

【问题讨论】:

  • 我已经编辑了我的答案,它很有可能成为我希望的解决方案 =)
  • 你也可以直接@user = User.find(params[:id]) || []

标签: ruby-on-rails ruby mongoid


【解决方案1】:

你的yml结构不对

必须是 -

development:
  sessions:
    options:
      #raise_not_found_error has to be not here but see below
  options: #strictly 2 spaces before
    raise_not_found_error: false #strictly 4 spaces before not 6

所以,raise_not_found_error 参数必须是 development>options 的子级,而不是 development>sessions>options

【讨论】:

  • 感谢这帮助我到达了配置级别所需的位置。但是现在它返回 null,而不是 JSON 格式。
  • respond_to { |format| format.json {render :json => @user} } 怎么样?
  • 给我:ActionController::UnknownFormat in UsersController#show 我正在使用 rails-api
  • 如果幕后一切都正确,它必须渲染正确的 json stackoverflow.com/questions/4253238/…
  • 如果你向下滚动,你会看到他正确设置了选项,你的回答并没有为发帖者的问题增加任何价值。
【解决方案2】:

对我来说,即使正确的缩进也不起作用,所做的是在 config/initializers/ 中创建一个名为 mongoid.rb 的初始化文件并将其放入其中

Mongoid.raise_not_found_error = false

【讨论】:

    【解决方案3】:

    为了节省几分钟,如果您仍然遇到问题并且确定您的 mongoid.yml 配置正确,请尝试停止 spring 服务器,因为它似乎做了很多缓存!

    $ spring stop

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多