如何解决Appsync解析器响应模板错误:无法转换为对象
我有一个appsync
速度响应映射模板,该模板返回的烦人错误
"Unable to convert \n \n,\n,\n \n\n{\"channels\":[{\"createdAt\":\"2020-09-01T14:25:32.796Z\",\"featured\":false,\"thumb\":\"http://www.google.com\",\"__typename\":\"Channel\",\"description\":\"The test channel\",\"id\":\"73bf0ef9-2d91-4b78-90f8-7548444fdb8f\",\"title\":\"The test title\",\"notifications\":false,\"tags\":[\"awesome\",\"action\"],\"updatedAt\":\"2020-09-01T14:25:32.796Z\"}],\"creators\":[],\"shows\":[{\"createdAt\":\"2020-09-01T14:40:46.996Z\",\"author\":\"Steven Colbert\",\"__typename\":\"Show\",\"description\":\"The tonight show with Steven Colbert\",\"id\":\"5a95234e-6d30-4396-bc05-b607eb4276f6\",\"source\":\"http://google.com\",\"title\":\"The tonight show\",\"channelId\":\"dce40626-76ae-45ba-97a9-a2b29d2234c9\",\"live\":false,\"tags\":[\"Tonight show\"],\"updatedAt\":\"2020-09-01T14:40:46.996Z\"},{\"createdAt\":\"2020-09-01T14:46:40.406Z\",\"author\":\"Seth Miers\",\"description\":\"The Late show with Seth Miers\",\"id\":\"09d3d573-8737-411c-ae82-ee3600565e04\",\"title\":\"The Late show\",\"tags\":[\"Late show\"],\"updatedAt\":\"2020-09-01T14:46:40.406Z\"}]}\n to Object."
我不明白为什么会这样?该对象似乎是正确的,并且graphQl模式期望一个具有三个属性的对象,每个属性应包含一个对象数组。
我已经检查了来自Elasticsearch请求的日志,并且从请求模板发送的响应正确。这是我从elasticsearch获得的对象:
{
"logType": "ResponseMapping","path": [
"search"
],"fieldName": "search","requestId": "c8946f14-8bb7-46a8-a23f-4b2defc0a9cd","context": {
"arguments": {
"filter": {
"title": {
"contains": "The"
}
}
},"result": {
"took": 36,"timed_out": false,"_shards": {
"total": 11,"successful": 11,"skipped": 1,"Failed": 0
},"hits": {
"total": {
"value": 3,"relation": "eq"
},"hits": [
{
"_index": "channel","_type": "_doc","_id": "73bf0ef9-2d91-4b78-90f8-7548444fdb8f","_score": 0,"_source": {
"createdAt": "2020-09-01T14:25:32.796Z","featured": false,"thumb": "http://www.google.com","__typename": "Channel","description": "The test channel","id": "73bf0ef9-2d91-4b78-90f8-7548444fdb8f","title": "The test title","notifications": false,"tags": [
"awesome","action"
],"updatedAt": "2020-09-01T14:25:32.796Z"
},"sort": [
"channel",0
]
},{
"_index": "show","_id": "5a95234e-6d30-4396-bc05-b607eb4276f6","_source": {
"createdAt": "2020-09-01T14:40:46.996Z","author": "Steven Colbert","__typename": "Show","description": "The tonight show with Steven Colbert","id": "5a95234e-6d30-4396-bc05-b607eb4276f6","source": "http://google.com","title": "The tonight show","channelId": "dce40626-76ae-45ba-97a9-a2b29d2234c9","live": false,"tags": [
"Tonight show"
],"updatedAt": "2020-09-01T14:40:46.996Z"
},"sort": [
"show","_id": "09d3d573-8737-411c-ae82-ee3600565e04","_source": {
"createdAt": "2020-09-01T14:46:40.406Z","author": "Seth Miers","description": "The Late show with Seth Miers","id": "09d3d573-8737-411c-ae82-ee3600565e04","title": "The Late show","tags": [
"Late show"
],"updatedAt": "2020-09-01T14:46:40.406Z"
},0
]
}
]
}
},"stash": {},"outErrors": []
},"fieldInError": false,"errors": [],"parentType": "Query"
}
这是我的响应映射模板。我既不喜欢VTL模板,也不喜欢VTL模板,但是我不明白为什么它抱怨不能将看似有效的对象转换为对象。
#set( $channels = [] )
#set( $creators = [] )
#set( $shows = [] )
#foreach($entry in $context.result.hits.hits)
## $veLocityCount starts at 1 and increments with the #foreach loop **
#if( $veLocityCount > 1 ),#end
#set ($item = $entry.get("_source"))
#set ($itemType = $entry.get("_index"))
#if ($itemType == "channel")
$util.qr($channels.add($item))
#end
#if ($itemType == "creator")
$util.qr($creators.add($item))
#end
#if ($itemType == "show")
$util.qr($shows.add($item))
#end
#end
#set ($response = { "channels": $channels,"creators": $creators,"shows": $shows } )
$util.toJson($response)
我也尝试了相同的结果和错误:
#set( $channels = [] )
#set( $creators = [] )
#set( $shows = [] )
#set( $hits = $context.result.hits.hits )
#set ($response = {} )
#foreach($entry in $hits)
## $veLocityCount starts at 1 and increments with the #foreach loop **
#if( $veLocityCount > 1 ),#end
#set ($item = $entry.get("_source") )
#set ($itemType = $entry.get("_index") )
#if ($itemType == "channel")
$util.qr( $channels.add($item) )
#end
#if ($itemType == "creator")
$util.qr( $creators.add($item) )
#end
#if ($itemType == "show")
$util.qr( $shows.add($item) )
#end
#end
$util.qr($response.put("channels",$channels))
$util.qr($response.put("creators",$creators))
$util.qr($response.put("shows",$shows))
$util.toJson($response)
我的graphQl模式的相关部分:
type Query @aws_api_key
@aws_iam {
search(filter: SearchFilterInput,size: Int,from: Int): SearchFilterConnectionModel
}
input SearchFilterInput {
title: ModelStringFilterInput
tag: ModelStringFilterInput
userName: String
}
type SearchFilterConnectionModel @aws_api_key
@aws_iam {
channels: [Channel]
shows: [ListShowConnectionModel]
creators: [Creator]
}
type ListShowConnectionModel @aws_api_key
@aws_iam {
id: ID!
createdAt: String
updatedAt: String
description: String
source: String
author: String
thumb: String
title: String
tags: [String!]
live: Boolean
channelId: String
}
type Channel @aws_api_key
@aws_iam {
id: ID!
createdAt: String
updatedAt: String
tags: [String!]
title: String
description: String
thumb: String
featured: Boolean
notifications: Boolean
}
type Creator @aws_api_key
@aws_iam {
id: ID!
createdAt: String
updatedAt: String
cognitoSub: String
userName: String
thumb: String
bio: String
channelsOwned: [ChannelsListModel!]
showsOwned: [ShowsListModel!]
}
解决方法
我能够重现您收到的错误,我认为问题出在您的foreach
语句中,您在其中两次访问hits
。
这是按预期方式工作的,完全正常:
#set( $channels = [] )
#set( $creators = [] )
#set( $shows = [] )
#set( $testCtx = [{"_source" : {"createdAt":"2020-09-01T14:25:32.796Z","featured":false,"thumb":"http://www.google.com","__typename":"Channel","description":"The test channel","id":"73bf0ef9-2d91-4b78-90f8-7548444fdb8f","title":"The test title","notifications":false,"tags":["awesome","action"],"updatedAt":"2020-09-01T14:25:32.796Z"},"_index": "channel"}] )
#foreach($entry in $testCtx)
## $velocityCount starts at 1 and increments with the #foreach loop **
#if( $velocityCount > 1 ),#end
#set ($item = $entry.get("_source"))
#set ($itemType = $entry.get("_index"))
#if ($itemType == "channel")
$util.qr($channels.add($item))
#end
#if ($itemType == "creator")
$util.qr($creators.add($item))
#end
#if ($itemType == "show")
$util.qr($shows.add($item))
#end
#end
#set ($response = { "channels": $channels,"creators": $creators,"shows": $shows } )
$util.toJson($response)
如果我将foreach
语句更改为#foreach($entry in $testCtx[0])
,则会收到与您同样的愚蠢的无法转换错误。除此之外,我已经对您的代码进行了几次测试,并且效果很好。
我希望这会帮助您或至少为您提供一些指出问题的指导。
,最后弄清楚了。
这是foreach循环中的那一行
#if( $velocityCount > 1 ),#end
这是在返回的项目列表中添加,
,使响应成为无效的json
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。