是什么导致了这个Ruby "can't convert Mongo::Cursor into Integer"错误?



我正在使用 Mongo Ruby 驱动程序,并且在我的代码中line 171之前和之后都有这段 Ruby 代码块,这显然是它下面错误的源头(query.each行是第 171 行):

    query = get_callback.call( "databases", id )
    if query.count > 0
      puts query.count.inspect + " results: " + query.inspect
      res = {}
      query.each do |result|
        puts result.inspect
      end
    else
      puts "No results" + res.inspect
      res = {}
    end

错误:

1 results: <Mongo::Cursor:0x3fc15642c154 namespace='myproj.databases' @selector={"_id"=>BSON::ObjectId('4fe120e4a2f9a386ed000001')} @cursor_id=>
TypeError - can't convert Mongo::Cursor into Integer:
        /Users/myuser/.rvm/gems/ruby-1.9.3-p194/gems/bson-1.6.4/lib/bson/byte_buffer.rb:156:in `pack'
        /Users/myuser/.rvm/gems/ruby-1.9.3-p194/gems/bson-1.6.4/lib/bson/byte_buffer.rb:156:in `put_int'
        /Users/myuser/.rvm/gems/ruby-1.9.3-p194/gems/mongo-1.6.4/lib/mongo/cursor.rb:603:in `construct_query_message'
        /Users/myuser/.rvm/gems/ruby-1.9.3-p194/gems/mongo-1.6.4/lib/mongo/cursor.rb:466:in `send_initial_query'
        /Users/myuser/.rvm/gems/ruby-1.9.3-p194/gems/mongo-1.6.4/lib/mongo/cursor.rb:459:in `refresh'
        /Users/myuser/.rvm/gems/ruby-1.9.3-p194/gems/mongo-1.6.4/lib/mongo/cursor.rb:128:in `next'
        /Users/myuser/.rvm/gems/ruby-1.9.3-p194/gems/mongo-1.6.4/lib/mongo/cursor.rb:291:in `each'
        /Users/myuser/Code/myproj/my_file.rb:171:in `block in initialize'

我的查询对象:{"_id"=>BSON::ObjectId('4fe120e4a2f9a386ed000001')}

我不知道是什么原因造成的。我已经验证了我找到的对象是否存在,并且query.count显示我的Mongo::Cursor中有结果。

我没有在谷歌上找到任何问题的例子,我发现网络上的每个Mongo/Ruby都像我一样使用each迭代器。 有人知道此错误的原因是什么吗?我注意到在尝试使用 to_a 将集合转换为 JSON 可用对象时,我也得到了它。


对于它的价值,以下是byte_buffer.rb的相关部分。与<<的线是line 156.

def put_int(i, offset=nil)
  @cursor = offset if offset
  if more?
    @str[@cursor, 4] = [i].pack(@int_pack_order)
  else
    ensure_length(@cursor)
    @str << [i].pack(@int_pack_order)
  end
  @cursor += 4
end

当您将 nil 传递给 Ruby Mongo 驱动程序limit()方法时,会发生这种情况。

相关内容

最新更新