node.js 将参数传递给 DynamoDB updateItem 方法
node.js passing a parameter to DynamoDB updateItem method
我想编写一个函数来更新 dynamodb 中的给定参数。
例如,在 dynamodb table 中,每个 userId 都是键,我的值类似于
{
"categoryname": "a",
"skillState": "a",
"skipcount": 1,
"userId": "amzn1.ask.account.xxx”
}
我想设置 "categoryname": "b"
尽管可能有 10-15 个这样的字段,所以我不想对字段名称进行硬编码。
function (userId,itemToUpdate,itemValue,callback) {
var updateExpressionString = "SET #"+itemToUpdate+" =:val1";
var expressionAtt = '#'+itemToUpdate + '';
console.log(updateExpressionString)
console.log(expressionAtt)
this.dynamodb.updateItem({
TableName: constants.dynamoDBDetailTableName,
Key: {
userId: {
S: userId
}
},
UpdateExpression: updateExpressionString,
ExpressionAttributeNames : {
expressionAtt : itemToUpdate
},
ExpressionAttributeValues : {
':val1': {'S':itemValue}
}
}, function (err, data) {
if (err) {
console.log(err)
console.log('Error ')
} else if (data.Item === undefined) {
}else {
console.log(data)
}
});
}
在 ExpressionAttributeNames 中:
{ ValidationException:ExpressionAttributeNames 包含无效键:语法错误;键:"expressionAtt"
这显然会抛出错误,认为 expressionAtt
是键,而它是局部变量。
我是 node.js 的新手,如何将局部变量传递给 ExpressionAttributeNames
和 ExpressionAttributeValues
处理此问题的一种方法是将对象从 updateItem
中拉出,将其放入自己的变量中,如下所示:
var item = {
TableName: constants.dynamoDBDetailTableName,
Key: {
userId: {
S: userId
}
},
UpdateExpression: updateExpressionString,
ExpressionAttributeNames: {},
ExpressionAttributeValue: {
':val1': {'S': itemValue}
}
};
item.ExpressionAttributeNames[expressionAtt] = itemToUpdate;
this.dynamodb.updateItem(item);
我相信这会解决您的问题
我想编写一个函数来更新 dynamodb 中的给定参数。
例如,在 dynamodb table 中,每个 userId 都是键,我的值类似于
{
"categoryname": "a",
"skillState": "a",
"skipcount": 1,
"userId": "amzn1.ask.account.xxx”
}
我想设置 "categoryname": "b"
尽管可能有 10-15 个这样的字段,所以我不想对字段名称进行硬编码。
function (userId,itemToUpdate,itemValue,callback) {
var updateExpressionString = "SET #"+itemToUpdate+" =:val1";
var expressionAtt = '#'+itemToUpdate + '';
console.log(updateExpressionString)
console.log(expressionAtt)
this.dynamodb.updateItem({
TableName: constants.dynamoDBDetailTableName,
Key: {
userId: {
S: userId
}
},
UpdateExpression: updateExpressionString,
ExpressionAttributeNames : {
expressionAtt : itemToUpdate
},
ExpressionAttributeValues : {
':val1': {'S':itemValue}
}
}, function (err, data) {
if (err) {
console.log(err)
console.log('Error ')
} else if (data.Item === undefined) {
}else {
console.log(data)
}
});
}
在 ExpressionAttributeNames 中:
{ ValidationException:ExpressionAttributeNames 包含无效键:语法错误;键:"expressionAtt"
这显然会抛出错误,认为 expressionAtt
是键,而它是局部变量。
我是 node.js 的新手,如何将局部变量传递给 ExpressionAttributeNames
和 ExpressionAttributeValues
处理此问题的一种方法是将对象从 updateItem
中拉出,将其放入自己的变量中,如下所示:
var item = {
TableName: constants.dynamoDBDetailTableName,
Key: {
userId: {
S: userId
}
},
UpdateExpression: updateExpressionString,
ExpressionAttributeNames: {},
ExpressionAttributeValue: {
':val1': {'S': itemValue}
}
};
item.ExpressionAttributeNames[expressionAtt] = itemToUpdate;
this.dynamodb.updateItem(item);
我相信这会解决您的问题