【问题标题】:DynamoDB put_item error ‘no such member’ (Ruby SDK)DynamoDB put_item 错误“没有这样的成员”(Ruby SDK)
【发布时间】:2019-04-15 04:23:25
【问题描述】:

我将 Ruby SDK aws-sdk-dynamodb 与 Ruby 2.5 一起用于 AWS Lambda 函数,该函数将项目保存到 AWS DynamoDB 表中。

我可以使用此代码成功保存项目:

    def save!
      hash = {
        table_name: ‘my-table’,
        item: {
          message_sid: '123456',
          created_at: Time.now.to_s
        }
      }
      dynamo = Aws::DynamoDB::Client.new(region: ‘us-east-1’)
      dynamo.put_item(hash)
      puts 'item successfully saved'
      true
    rescue => error
      puts "Unable to save item: #{error}: #{error.message}"
      false
    end

当我使用此代码时,我收到一个错误“没有这样的成员:message_sid”:

    def save!
      dynamoDB = Aws::DynamoDB::Resource.new(region: ‘us-east-1’)
      table = dynamoDB.table(‘my-table’)
      hash = { message_sid: '123456', created_at: Time.now.to_s }
      table.put_item(hash)
      puts 'item successfully saved'
      true
    rescue => error
      puts "Unable to save item: #{error}: #{error.message}"
      false
    end

我没有找到任何有关“没有这样的成员”错误的 DynamoDB 文档。为什么第二个例子失败了?

【问题讨论】:

    标签: ruby amazon-dynamodb aws-sdk-ruby


    【解决方案1】:

    诚然,错误消息不是很有帮助,但仔细阅读 example documentation 会发现,当在表对象上调用方法 put_item 时,DynamoDB 需要键 item:。所以这段代码可以工作:

        def save!
          dynamoDB = Aws::DynamoDB::Resource.new(region: ‘us-east-1’)
          table = dynamoDB.table(‘my-table’)
          hash = { message_sid: '123456', created_at: Time.now.to_s }
          table.put_item(item: hash)
          puts 'item successfully saved'
          true
        rescue => error
          puts "Unable to save item: #{error}: #{error.message}"
          false
        end
    
    

    具体来说,您应该使用table.put_item(item: hash) 而不是table.put_item(hash)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-10
      • 2012-08-19
      • 1970-01-01
      • 2023-03-08
      相关资源
      最近更新 更多