node.js - 在 UpdateExpression 上使用 DELETE Set 更新 DynamoDB 记录失败并出现 node.js
问题描述
我正在尝试使用下面代码片段上的有效负载对DynamoDB.DocumentClient
实例执行更新调用:AWS SDK
const AWS = require('aws-sdk')
const DynamoDB = new AWS.DynamoDB.DocumentClient()
...
const TableName = 'MyTable'
const Key = { PK: 'MyPK', SK: 'MySK' }
const operation = 'DELETE'
const myId = 'abcde'
const currentRecord = await DynamoDB.get({TableName, Key)
DynamoDB.update({
TableName,
Key,
UpdateExpression: `
${operation} myIds :valuesToModify,
version :incrementVersionBy
`,
ConditionExpression: `version = :version`,
ExpressionAttributeValues: {
":version": currentRecord.version,
":incrementVersionBy": 1,
":valuesToModify": DynamoDB.createSet([myId])
}
})...
结果我收到此错误:
ERROR Invoke Error
{
"errorType":"Error",
"errorMessage":"ValidationException: Invalid UpdateExpression: Incorrect operand type for operator or function;
operator: DELETE, operand type: NUMBER, typeSet: ALLOWED_FOR_DELETE_OPERAND",
"stack":[...]
}
有趣的是,如果operation
更改为ADD
它运行良好。
非常感谢任何有助于理解为什么ADD
有效和无效的线索DELETE
和/或如何修复和/或与此更新操作兼容的替代方法!
解决方案
这里唯一可能的解决方法是不使用 DELETE 操作,相反,您必须查询该项目,在要删除的数组中找到索引,然后使用 REMOVE 操作将其删除:
就像在这种情况下,arrayField 包含一个用户数组,我想通过用户的电话号码删除。
const dataStore = await dynamodb.get(queryParams).promise();
let i=0; //save the index
for(i = 0; i < dataStore.Item.myTable.length; i++){
if(dataStore.Item.arrayField[i].phone === phoneNumber)
{
break;
}
}
if(i < dataStore.Item.arrayField.length){
const updateStoreParams = {
TableName: tableName,
Key: storeTableKey,
UpdateExpression: `REMOVE arrayField[${i}]`,
}
await dynamodb.update(updateStoreParams).promise().catch((err) => {
console.log(err);
throw err;
});
}
推荐阅读
- java - 为什么我的代码没有返回我想要它做的事情?
- c# - 括号中的正则表达式搜索关键字
- reactjs - 使用过滤器功能查找 TypeScript 属性
- laravel - 为什么 Laravel 控制器 store() 将空值插入 DB
- ruby-on-rails-5 - EnvironmentMismatchError - Rails.env 被设置为“生产”
- algorithm - 对于 m 的哪些值(以 n 表示)O(m*n) 比 O(n^2) 好(为什么)?
- ruby-on-rails - 如何在 Ruby 中将 POLYGONS 与 MULTIPOLYGON 分开
- python - 在 groupby 之后基于第二列中的第一个最大值的一列的值
- node.js - 如何在 MS Teams 中使用轮播卡显示分页
- python - Python / Flask:ImportError:无法从'core'导入名称'app'