gpt4 book ai didi

javascript - 如何使用 putItem 替换 Node.js Simpledb 中的属性值

转载 作者:行者123 更新时间:2023-11-28 20:37:04 24 4
gpt4 key购买 nike

我有一个 Node 应用程序将数据保存到 Amazon SimpleDb,以使用 rjrodger/simpledb Node 驱动程序 ( https://github.com/rjrodger/simpledb ) 存储名称/值对数据。我调用 putItem 如下:

sdb.putItem('mySimpleDbTable','Record_0001', 
{
statusUpdate:'1'
},
function(err,res,meta){
if (err) {
console.log('Status Update error: ' + err.Message);
}
console.log("Memories, you're talking about memories: "+JSON.stringify(res))
})

插入如下记录:

{ $ItemName: 'Record_0001' { statusUpdate: '1' }}

经过几次操作后,我想将 statusUpdate 更新为“2”,如下所示:

{ $ItemName: 'Record_0001' { statusUpdate: '2' }}

无论我如何尝试,Node simpleDb 驱动程序总是插入一个新属性,如下所示:

{ $ItemName: 'Record_0001' { statusUpdate: '1',  statusUpdate: '2' }}

如果我尝试

sdb.putItem('mySimpleDbTable','Record_0001', 
{
statusUpdate:['2', true]
}

然后我得到 -

{ $ItemName: 'Record_0001' { statusUpdate: '1',  statusUpdate: '2', statusUpdate: 'true' }}

如何使用 putItem 的“replace:true”参数来更新值而不是插入新值。

请帮忙。

最佳答案

您的代码始终将 Replace 参数设置为 false,因此这些值将添加到现有值中。如果将其设置为 true,则属性的现有值始终会被新值替换。即

Optionally, you can supply the Replace parameter for each individual attribute. Setting this value to true causes the new attribute value to replace the existing attribute value(s) if any exist. Otherwise, Amazon SimpleDB simply inserts the attribute values. For example, if an item has the attributes { 'a', '1' }, { 'b', '2'}, and { 'b', '3' } and the requester calls BatchPutAttributes using the attributes { 'b', '4' } with the Replace parameter set to true, the final attributes of the item are changed to { 'a', '1' } and { 'b', '4' }. This occurs because the new 'b' attribute replaces the old value.

--Source

statusUpdate:['2', true]

这里的true不是那个替换参数。查看结果。代码将 true 作为该属性的值。

关于javascript - 如何使用 putItem 替换 Node.js Simpledb 中的属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15169729/

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