riak content_type没有方法错误

时间:2012-06-25 05:57:18

标签: ruby riak nomethoderror

我的os x机器上运行了4节点riak设置。我有以下程序 -

require 'riak'




class RiakClient < Riak::Client
    #attr_accessor :bucket

    def initialize(hosts="")
        return Riak::Client.new(:nodes => [{:http_port => 8091},{:http_port =>8092},{:http_port=>8093},{:http_port =>8094}])
    end

    def get_me(bucket, key)
        obj = self.bucket(bucket).get(key)
        puts obj.data
    end

    def put_me(bucket, key, data, content_type)
        obj=self.bucket(bucket).get_or_new(key)
        puts obj.class
        obj.content_type=content_type
        obj.raw_data=data
        obj.store
    end
end



if __FILE__ == $0
    my_client=RiakClient.new
    my_client.put_me("doc", "index.html", "some data goes here", "text/html")
    hash=my_client.get_me("doc", "index.html")
end

我收到以下错误

NilClass
riak_client.rb:32:in `put_me': undefined method `content_type=' for nil:NilClass (NoMethodError)
    from riak_client.rb:42:in `<main>'

我是否必须导入RiakBucket和RiakObject类?看来RiakBucket方法在这里无法访问?

1 个答案:

答案 0 :(得分:1)

这里的实际问题是:为什么get_or_new会返回nil?

这可能是因为你的initialize()方法返回一个新的Riak :: Client,但是作为父对象的Riak :: Client永远不会被初始化。尝试拨打超级电话,而不是return Riak::Client.new中的initialize

子类Riak :: Client在这里有点可疑。我倾向于委托给它。