問題描述
我能夠在沒有 :status : 接受表達式屬性值的情況下獲得響應,但是有了它,當我在 projectionExpression 行中使用 #status 時出現以下錯誤(status 是 DynamoDB 中的保留字,所以我有按 https://stackoverflow.com/a/45952329/5921575 在那里添加主題標簽):
I am able to get a response without the :status : accept expression attribute value but with it, I get the following error when I am using the #status in the projectionExpression line (status is a reserved word in DynamoDB so I had to add hashtag there per https://stackoverflow.com/a/45952329/5921575):
Error Domain=com.amazonaws.AWSCognitoIdentityErrorDomain Code=0 "(null)"
UserInfo={__type=com.amazon.coral.validate#ValidationException,
message=Value provided in ExpressionAttributeValues unused in expressions: keys: {:status}}
代碼如下:
queryExpression.keyConditionExpression = "#userId= :userId"
queryExpression.expressionAttributeNames = ["#userId":"userId", "#status":"status"]
queryExpression.expressionAttributeValues = [":userId":userID, ":status":"accept"]
queryExpression.projectionExpression = "#status"
我可以不用 ":status":"accept" 但我不想得到很多沒有接受值的項目.我在此鏈接或 stackoverflow 上的任何地方都找不到答案:http://docs.aws.amazon.com/amazondynamodb/最新/developerguide/Expressions.ExpressionAttributeNames.html
I can go without the ":status":"accept" but I do not want to get a lot of items that do not have the accept value. I can't find an answer in this link or anywhere on stackoverflow: http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.ExpressionAttributeNames.html
謝謝!
推薦答案
有點晚了,但是:你的投影表達式不應該是#status",而是另一個不是狀態的詞.Status 是保留字,所以不要將它用于投影表達式.有關需要使用保留字時該怎么做的文檔,請參閱此處:https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.ExpressionAttributeNames.html#Expressions.ExpressionAttributeNames.ReservedWords
A bit late, but: Your projection expression should not be "#status" but another word that isn't status. Status is the reserved word, so don't use that for the projection expression. See here for docs on what to do when you need to use a reserved word: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.ExpressionAttributeNames.html#Expressions.ExpressionAttributeNames.ReservedWords
另一方面,userId 不需要投影表達式,因為它不是保留字.請參閱此處獲取保留字列表:https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ReservedWords.html
userId, on the other hand, does not require a projection expression because it is not a reserved word. See here for a list of reserved words: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ReservedWords.html
但是,您不需要投影表達式.您可以簡單地使用下面的代碼.定義queryExpression.expressionAttributeNames"以創建狀態屬性值的替代名稱.在這里,我使用了短語statusVal"作為替代.
However, you don't need a projection expression. You can simply use the code below. Define "queryExpression.expressionAttributeNames" to create a substitute name for the status attribute value. Here, I used the phrase "statusVal" as a substitute.
試試這個.(它對我有用)
Try this. (It worked for me)
let queryExpression = AWSDynamoDBQueryExpression()
queryExpression.expressionAttributeNames = ["#statusVal":"status"] // Using statusVal because it is not reserved. You only need statusVal here because it is the only attribute that also happens to be an AWS reserved word.
queryExpression.keyConditionExpression = "userId = :uId AND #statusVal = :sV"
queryExpression.expressionAttributeValues = [
":uId" : String(describing: userId),
":sV" : "accept"]
然后使用 AWSDynamoDBObjectMapper 執行操作!祝你好運!
And then perform the operation using AWSDynamoDBObjectMapper! Good luck!
這篇關于使用 Swift 3 在 DynamoDB 中保留關鍵字 ExpressionAttributeValues的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!