gpt4 book ai didi

javascript - 删除 dynamodb 中的列表元素,索引是存储在变量中的值

转载 作者:行者123 更新时间:2023-11-29 10:29:23 24 4
gpt4 key购买 nike

我正在尝试从列表中删除一个项目。当我对项目的索引进行硬编码(即 REMOVE relatedUsers[0])但我在变量中找到索引时,更新表达式起作用。因此,我尝试使用 ExpressionAttributeValues 替换更新表达式中的变量,但出现错误“无效的 UpdateExpression:语法错误; token :\":userIndex\",附近:\"[:userIndex]\"'

这是我的代码

function updateUser(data) {
console.log('---------updateUser---------');
console.log(data);
const params = {
TableName: process.env.USER_TABLE,
Key: {
id: data.id,
},
ExpressionAttributeValues: {
':updatedAt': timestamp,
':notificationCount':1,
':userIndex':data.index,
},
UpdateExpression: 'ADD notificationCount :notificationCount REMOVE relatedUsers[:userIndex] SET updatedAt= :updatedAt ',
ReturnValues: 'ALL_NEW',
};

return new Promise((resolve, reject)=>{
dynamodb.update(params, (error,data) => {
// handle potential errors
if (error) {
reject(error);
}
else {
console.log("update consultant response");
console.log(data);
resolve(data);
}
});
});
}

我也试过 ExpressionAttributeNames

function updateUser(data) {
console.log('---------updateUser---------');
console.log(data);
let relatedUser = 'relatedUsers[' + data.index + ']'
const params = {
TableName: process.env.USER_TABLE,
Key: {
id: data.id,
},
ExpressionAttributeNames: {
'#user':relatedUser
},
ExpressionAttributeValues: {
':updatedAt': timestamp,
':notificationCount':1,
},
UpdateExpression: 'ADD notificationCount :notificationCount REMOVE #user SET updatedAt= :updatedAt ',
ReturnValues: 'ALL_NEW',
};

但它没有更新数据库中的任何内容。你能帮我解决这个问题吗?

最佳答案

实际上你只是在构建一个查询字符串,所以尝试使用文字代替:

function updateUser(data) {
console.log('---------updateUser---------');
console.log(data);
const params = {
TableName: process.env.USER_TABLE,
Key: {
id: data.id,
},
ExpressionAttributeValues: {
':updatedAt': timestamp,
':notificationCount':1
},
UpdateExpression: "ADD notificationCount :notificationCount REMOVE relatedUsers[" + data.index + "] SET updatedAt= :updatedAt",
ReturnValues: 'ALL_NEW',
};

return new Promise((resolve, reject)=>{
dynamodb.update(params, (error,data) => {
// handle potential errors
if (error) {
reject(error);
}
else {
console.log("update consultant response");
console.log(data);
resolve(data);
}
});
});
}

关于javascript - 删除 dynamodb 中的列表元素,索引是存储在变量中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50112218/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com