DynamoDB中的UpdateItem错误ValidationException:无效的属性值类型

问题描述 投票:1回答:1

我的DynamoDB中有一个名为JuridicalPerson的表

var params = {
  AttributeDefinitions: [{
    AttributeName: 'code',
    AttributeType: 'S'
  }],
  KeySchema: [{
    AttributeName: 'code',
    KeyType: 'HASH'
  }],
  ProvisionedThroughput: {
    ReadCapacityUnits: 5,
    WriteCapacityUnits: 5
  },
  TableName: 'JuridicalPerson'
}

我可以在此处保存项目,但不能更新这些项目。

JuridicalPerson表中的项目示例

{
   "code": {
     "S": "jp_rJaHvVrzf"
   },
   "status": {
     "S": "pending"
   }
}

更新表达式

function updateDynamoDB (payload) {
  const adhesionUpdate = Object.assign({}, payload)

  return new Promise((resolve, reject) => {
    const params = {
      TableName: 'JuridicalPerson',
      Key: {
        'code': {
          'S': adhesionUpdate.code
        }
      },
      UpdateExpression: 'SET #status = :val1',
      ExpressionAttributeNames: {
        '#status': 'status'
      },
      ExpressionAttributeValues: {
        ':val1': { 'S': adhesionUpdate.status }
      },
      ReturnValues: 'ALL_NEW'
    }

    return dynamoAdapter.getState().update(params, (err, items) => {
      if (err) {
        return reject(err)
      }
      return resolve(items)
    })
  })
}

如果我在更新前放置console.log只是为了查看params,我们就有

params:  { TableName: 'JuridicalPerson',
  Key: { code: { S: 'jp_rJaHvVrzf' } },
  UpdateExpression: 'set #status = :val1',
  ExpressionAttributeNames: { '#status': 'status' },
  ExpressionAttributeValues: { ':val1': { S: 'active' } },
  ReturnValues: 'ALL_NEW' }

但是我遇到了以下错误

err:  { ValidationException: Invalid attribute value type
  message: 'Invalid attribute value type',
  code: 'ValidationException',
  time: 2017-12-18T12:40:39.488Z,
  requestId: 'bc23aab1-d9a5-426f-a1af-3ff558e7e0fa',
  statusCode: 400,
  retryable: false,
  retryDelay: 41.054909592801195 }
node.js amazon-dynamodb
1个回答
1
投票

使用DynamoDb客户端而不是DynamoDb资源时,我有相同的模棱两可的错误消息。

DDB客户端接受DDB资源key: value接受的简短形式key: {'S': value}而不是显式类型形式的值的定义。

在您的示例中,尝试使用:

const params = {
  TableName: 'JuridicalPerson',
  Key: {
    'code': adhesionUpdate.code
  },
  UpdateExpression: 'SET #status = :val1',
  ExpressionAttributeNames: {
    '#status': 'status'
  },
  ExpressionAttributeValues: {
    ':val1': adhesionUpdate.status
  },
  ReturnValues: 'ALL_NEW'
}
© www.soinside.com 2019 - 2024. All rights reserved.