Javascript DynamoDB 中的 Scan 函数,保留关键字为 FilterExpression NodeJS

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36698945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:27:23  来源:igfitidea点击:

Scan Function in DynamoDB with reserved keyword as FilterExpression NodeJS

javascriptnode.jsamazon-web-servicesamazon-dynamodb

提问by Saleem Ahmed

My scan function :

我的扫描功能:

var tableName = 'faasos_orders',
    filterExp = 'status = :delivered OR status = :void OR status = :bad',
    projectionValues = '',
    expressionAttr = {};    
    expressionAttr[":delivered"] = "delivered";
    expressionAttr[":bad"] = "bad";
    expressionAttr[":void"] = "void"; 
    limit = 10;
  dynamoConnector.getItemUsingScan(tableName, filterExp, projectionValues, expressionAttr, function (err, data) {  ...........} 

Error on running :

运行时出错:

    { [ValidationException: Invalid FilterExpression: Attribute name is a reserved keyword; reserved keyword: status]
  message: 'Invalid FilterExpression: Attribute name is a reserved keyword; reserved keyword: status',
  code: 'ValidationException',
  time: Mon Apr 18 2016 21:57:30 GMT+0530 (IST),
  requestId: 'AV6QFHM7SPQT1QR3D4OO81ED4FVV4KQNSO5AEMVJF66Q9ASUAAJG',
  statusCode: 400,
  retryable: false,
  retryDelay: 0 }

Now I do get the point I am trying to use a reserved keyword in th e filterExpression which is illegal. But if I run the same function through aws gui it returns data beautifully (check image for details): Scan function on status through gui

现在我明白了,我试图在过滤器表达式中使用一个非法的保留关键字。但是如果我通过 aws gui 运行相同的函数,它会很好地返回数据(查看图像了解详细信息): 通过 gui 扫描状态函数

So the question is how do I add the filter expression through node without having to change the key name ???

所以问题是如何通过节点添加过滤器表达式而不必更改键名???

回答by Saleem Ahmed

Solved :

解决了 :

There are two parameters taken by aws-sdk :

aws-sdk 有两个参数:

Expression Attribute Name

表达式属性名称

Expression Attribute Value

表达式属性值

both provide the functionality of replacing placeholders used in the attributes list. Here by Attributes it is a bit ambiguous, where I got confused. The wizards over at aws mean both the key and value when they use the term attribute.

两者都提供替换属性列表中使用的占位符的功能。这里的 Attributes 有点模棱两可,让我感到困惑。aws 上的向导在使用术语属性时表示键和值。

So in a case where you want to use a reserved key word as a key attribute use the Expression Attribute Name parameter with #(pound) to denote the placeholder.

因此,如果您想使用保留关键字作为关键属性,请使用带有 #(pound) 的 Expression Attribute Name 参数来表示占位符。

Similarly where you want to use placeholders for value attribute use the Expression Attribute Value parameter with :(colon) to denote the placeholder.

类似地,您要为 value 属性使用占位符,请使用带有 :(colon) 的 Expression Attribute Value 参数来表示占位符。

So finally my code (working) looks like this :

所以最后我的代码(工作)看起来像这样:

var param = {
  TableName: "faasos_orders",
  FilterExpression: "#order_status = :delivered OR #order_status = :void OR #order_status = :bad",
  ExpressionAttributeValues: {
    ":delivered": "delivered",
    ":void": "void",
    ":bad": "bad"
  }
  ExpressionAttributeNames: {
    "#order_status": "status"
  }
};  
  dynamodb.scan(param, function (err, data) {....});

回答by Mark B

:statusis a placeholder in your expression that you aren't providing a value for. See how you are providing values for your other placeholders here:

:status是表达式中的占位符,您没有为其提供值。在此处查看您如何为其他占位符提供值:

expressionAttr[":delivered"] = "delivered";
expressionAttr[":bad"] = "bad";
expressionAttr[":void"] = "void"

You need to do the same for the :statusplaceholder. I don't see anything about a reserved word in the error message, so I'm not sure why you think that's the cause of the error. The error very specifically states that you aren't providing a value for the :statusplaceholder.

您需要对:status占位符执行相同的操作。我在错误消息中没有看到任何关于保留字的信息,所以我不确定为什么你认为这是错误的原因。该错误非常明确地指出您没有为:status占位符提供值。