我能够得到没有:status:accept表达式属性值的响应,但是当我在projectionExpression行中使用#status时,我得到以下错误(状态是DynamoDB中的保留字,所以我有根据{{3}})添加#标签:
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"
我可以没有":状态":"接受"但我不想得到很多没有接受价值的物品。我无法在此链接或stackoverflow上的任何位置找到答案: https://stackoverflow.com/a/45952329/5921575
谢谢!
答案 0 :(得分:1)
有点晚了,但是:您的投影表达式不应为“ #status”,而应为非状态的其他单词。状态是保留字,因此请勿将其用于投影表达式。有关需要使用保留字时的处理方法,请参见此处的文档: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
但是,您不需要投影表达式。您可以简单地使用下面的代码。定义“ queryExpression.expressionAttributeNames”以创建状态属性值的替代名称。在这里,我用“ statusVal”代替。
尝试一下。 (对我有用)
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执行操作!祝你好运!