错误TypeError:无法将BSON :: Document转换为String

时间:2015-06-28 17:34:05

标签: ruby mongodb curl

我有一个mongodb连接的sinatra,我收到了这个错误

  

“ERROR TypeError:无法将BSON :: Document转换为字符串”

当我尝试使用insert_one命令时。要尝试post操作,我使用curl和这个命令:

  

curl -i -F project_id = Test_project -F filedata=@jmeter.log   http://myserver/v1/upload/media

我的代码是:

post "/v1/upload/media" do
        #Return 400  If project_id does not exist
        return status 400 if params['project_id'].empty?

        files_h = Hash.new
        files_collect = settings.mongo_db[:media]

        File.open('uploads/' + params['filedata'][:filename], "w") do |f|
            puts "File size #{params['filedata'][:tempfile].size}"
            f.write(params['filedata'][:tempfile].read)
            files_h['name'] = params['filedata'][:filename]
            files_h['sha1'] =  Digest::SHA1.file(f).hexdigest
        end
        files_h['project_id'] = params['project_id']
        puts files_h
        puts params
        #Check if the file exist
        if files_collect.find(:name => files_h['name']).count == 0
                #Inserting the data into DB
                 settings.mongo_db[:media].insert_one(files_h)
        else

        end
    end

服务器日志也是:

D, [2015-06-28T19:21:14.321294 #8781] DEBUG -- : MONGODB | COMMAND | namespace=kalliopeDB.$cmd selector={:insert=>"files", :documents=>[{"name"=>"jmeter.log", "sha1"=>"bf4272abce2a0c10a01a1dfc53187d4e780da397", "project_id"=>"Test_project", :_id=><BSON::ObjectId:0x11811820 data=55902d0a6f7269224d000000>}], :writeConcern=>{:w=>1}, :ordered=>true} flags=[] limit=-1 skip=0 project=nil | runtime: 0.4694ms
[2015-06-28 19:21:14] ERROR TypeError: can't convert BSON::Document into String
    /usr/local/rvm/gems/ruby-1.9.3-p551/gems/rack-1.6.1/lib/rack/handler/webrick.rb:114:in `block in service'
    /usr/local/rvm/gems/ruby-1.9.3-p551/gems/mongo-2.0.6/lib/mongo/operation/result.rb:119:in `each'
    /usr/local/rvm/gems/ruby-1.9.3-p551/gems/mongo-2.0.6/lib/mongo/operation/result.rb:119:in `each'
    /usr/local/rvm/gems/ruby-1.9.3-p551/gems/rack-1.6.1/lib/rack/handler/webrick.rb:113:in `service'
    /usr/local/rvm/rubies/ruby-1.9.3-p551/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
    /usr/local/rvm/rubies/ruby-1.9.3-p551/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
    /usr/local/rvm/rubies/ruby-1.9.3-p551/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'

关于什么是问题的任何想法?,我有其他insert_one代码使用哈希没有问题。

提前致谢。

1 个答案:

答案 0 :(得分:0)

最后添加“返回状态200”解决了类型错误,似乎sinatra发生了一些事情,即尝试将生成的BSON :: Document转换为字符串。