为什么在使用 Appsync 和 DynamoDB 时出现无效的 UpdateExpression:语法错误?
Why do I get Invalid UpdateExpression: Syntax error when using Appsync & DynamoDB?
我是 AppSync 和 DynamoDB 的新手,很抱歉这个问题很基础。我有一个适用于此架构的 AppSync 解析器:
type Profile {
name: String!
location: String!
}
input ProfileInput {
name: String!
location: String!
}
type Mutation {
createProfile(profileInput: ProfileInput!): Profile! @aws_cognito_user_pools @aws_api_key
}
解析器是:
请求
{
"version" : "2017-02-28",
"operation" : "UpdateItem",
"key" : {
"name" : $util.dynamodb.toDynamoDBJson($ctx.identity.username),
"attribute": $util.dynamodb.toDynamoDBJson("info")
},
"update" : {
"expression" : "SET #p :da",
"expressionNames" : {
"#p" : "data"
},
"expressionValues" : {
":da" : $util.dynamodb.toDynamoDBJson($ctx.args.profileInput)
}
}
}
回应
$util.toJson($ctx.result)
我发送到 Appsync 的请求 :
mutation CreateProfileMutation {
createProfile(profileInput: {location: "Updated Location", name: "Updated Username"}) {
location
name
}
}
但是 Dynamodb 抛出了这个错误:
{
"data": null,
"errors": [
{
"path": [
"createProfile"
],
"data": null,
"errorType": "DynamoDB:DynamoDbException",
"errorInfo": null,
"locations": [
{
"line": 12,
"column": 3,
"sourceName": null
}
],
"message": "Invalid UpdateExpression: Syntax error; token: \":da\", near: \"#p :da\" (Service: DynamoDb, Status Code: 400, Request ID: some id, Extended Request ID: null)"
}
]
}
转换后的模板为:
{
"version" : "2017-02-28",
"operation" : "UpdateItem",
"key" : {
"name" : {"S":"user#1"},
"attribute": {"S":"info"}
},
"update" : {
"expression" : "SET #p :da",
"expressionNames" : {
"#p" : "data"
},
"expressionValues" : {
":da" : {"M":{"name":{"S":"Updated Username"},"location":{"S":"Updated Location"}}}
}
}
}
看起来像是语法错误,但我无法弄清楚错误是什么。感谢任何帮助。
更新表达式中的 SET
操作缺少 =
操作数。
应该是"expression" : "SET #p = :da"
而不是"expression" : "SET #p :da"
。
我是 AppSync 和 DynamoDB 的新手,很抱歉这个问题很基础。我有一个适用于此架构的 AppSync 解析器:
type Profile {
name: String!
location: String!
}
input ProfileInput {
name: String!
location: String!
}
type Mutation {
createProfile(profileInput: ProfileInput!): Profile! @aws_cognito_user_pools @aws_api_key
}
解析器是:
请求
{
"version" : "2017-02-28",
"operation" : "UpdateItem",
"key" : {
"name" : $util.dynamodb.toDynamoDBJson($ctx.identity.username),
"attribute": $util.dynamodb.toDynamoDBJson("info")
},
"update" : {
"expression" : "SET #p :da",
"expressionNames" : {
"#p" : "data"
},
"expressionValues" : {
":da" : $util.dynamodb.toDynamoDBJson($ctx.args.profileInput)
}
}
}
回应
$util.toJson($ctx.result)
我发送到 Appsync 的请求 :
mutation CreateProfileMutation {
createProfile(profileInput: {location: "Updated Location", name: "Updated Username"}) {
location
name
}
}
但是 Dynamodb 抛出了这个错误:
{
"data": null,
"errors": [
{
"path": [
"createProfile"
],
"data": null,
"errorType": "DynamoDB:DynamoDbException",
"errorInfo": null,
"locations": [
{
"line": 12,
"column": 3,
"sourceName": null
}
],
"message": "Invalid UpdateExpression: Syntax error; token: \":da\", near: \"#p :da\" (Service: DynamoDb, Status Code: 400, Request ID: some id, Extended Request ID: null)"
}
]
}
转换后的模板为:
{
"version" : "2017-02-28",
"operation" : "UpdateItem",
"key" : {
"name" : {"S":"user#1"},
"attribute": {"S":"info"}
},
"update" : {
"expression" : "SET #p :da",
"expressionNames" : {
"#p" : "data"
},
"expressionValues" : {
":da" : {"M":{"name":{"S":"Updated Username"},"location":{"S":"Updated Location"}}}
}
}
}
看起来像是语法错误,但我无法弄清楚错误是什么。感谢任何帮助。
更新表达式中的 SET
操作缺少 =
操作数。
应该是"expression" : "SET #p = :da"
而不是"expression" : "SET #p :da"
。