dherault / serverless-offline

Emulate AWS λ and API Gateway locally when developing your Serverless project
MIT License
5.2k stars 794 forks source link

Template error Cannot read property 'toString' of undefined on $util.escapeJavaScript($map.get($key)) #101

Closed robhorvath closed 8 years ago

robhorvath commented 8 years ago

Using serverless 1.0.0-beta.2 and the latest serverless-offline v1 branch, I receive this error on every request. Any ideas?

Serverless: Error while parsing template "application/json" for getCategories
[ 'Error: Cannot read property \'toString\' of undefined on $util.escapeJavaScript($map.get($key)) at L/N 5:5',
  'at Velocity.utils.mixin.getPropMethod (/Users/robert.horvath/code/pos-datamanagement/server/node_modules/velocityjs/src/compile/references.js:293:19)',
  'at Velocity.utils.mixin.getAttributes (/Users/robert.horvath/code/pos-datamanagement/server/node_modules/velocityjs/src/compile/references.js:190:20)',
  'at Velocity.<anonymous> (/Users/robert.horvath/code/pos-datamanagement/server/node_modules/velocityjs/src/compile/references.js:132:22)',
  'at Array.some (native)',
  'at Object.utils.(anonymous function) [as some] (/Users/robert.horvath/code/pos-datamanagement/server/node_modules/velocityjs/src/utils.js:9:25)',
  'at Velocity.utils.mixin.getReferences (/Users/robert.horvath/code/pos-datamanagement/server/node_modules/velocityjs/src/compile/references.js:129:15)',
  'at Velocity.<anonymous> (/Users/robert.horvath/code/pos-datamanagement/server/node_modules/velocityjs/src/compile/compile.js:80:37)',
  'at Array.forEach (native)',
  'at Object.utils.(anonymous function) [as forEach] (/Users/robert.horvath/code/pos-datamanagement/server/node_modules/velocityjs/src/utils.js:9:25)',
  'at Velocity.utils.mixin._render (/Users/robert.horvath/code/pos-datamanagement/server/node_modules/velocityjs/src/compile/compile.js:71:13)',
  'at Velocity.<anonymous> (/Users/robert.horvath/code/pos-datamanagement/server/node_modules/velocityjs/src/compile/blocks.js:218:21)',
  'at Array.forEach (native)',
  'at Object.utils.(anonymous function) [as forEach] (/Users/robert.horvath/code/pos-datamanagement/server/node_modules/velocityjs/src/utils.js:9:25)',
  'at Velocity.utils.mixin.getBlockEach (/Users/robert.horvath/code/pos-datamanagement/server/node_modules/velocityjs/src/compile/blocks.js:200:13)',
  'at Velocity.utils.mixin.getBlock (/Users/robert.horvath/code/pos-datamanagement/server/node_modules/velocityjs/src/compile/blocks.js:21:22)',
  'at Velocity.<anonymous> (/Users/robert.horvath/code/pos-datamanagement/server/node_modules/velocityjs/src/compile/compile.js:103:57)',
  'at Array.forEach (native)',
  'at Object.utils.(anonymous function) [as forEach] (/Users/robert.horvath/code/pos-datamanagement/server/node_modules/velocityjs/src/utils.js:9:25)',
  'at Velocity.utils.mixin._render (/Users/robert.horvath/code/pos-datamanagement/server/node_modules/velocityjs/src/compile/compile.js:71:13)',
  'at Velocity.utils.mixin.getReferences (/Users/robert.horvath/code/pos-datamanagement/server/node_modules/velocityjs/src/compile/references.js:102:23)',
  'at Velocity.<anonymous> (/Users/robert.horvath/code/pos-datamanagement/server/node_modules/velocityjs/src/compile/compile.js:80:37)',
  'at Array.forEach (native)',
  'at Object.utils.(anonymous function) [as forEach] (/Users/robert.horvath/code/pos-datamanagement/server/node_modules/velocityjs/src/utils.js:9:25)',
  'at Velocity.utils.mixin._render (/Users/robert.horvath/code/pos-datamanagement/server/node_modules/velocityjs/src/compile/compile.js:71:13)',
  'at Velocity.utils.mixin.render (/Users/robert.horvath/code/pos-datamanagement/server/node_modules/velocityjs/src/compile/compile.js:37:22)',
  'at renderVelocityString (/Users/robert.horvath/code/pos-datamanagement/server/node_modules/serverless-offline/src/renderVelocityTemplateObject.js:30:80)',
  'at renderVelocityTemplateObject (/Users/robert.horvath/code/pos-datamanagement/server/node_modules/serverless-offline/src/renderVelocityTemplateObject.js:87:46)',
  'at server.route.handler.error (/Users/robert.horvath/code/pos-datamanagement/server/node_modules/serverless-offline/src/index.js:404:25)',
  'at Object.internals.handler (/Users/robert.horvath/code/pos-datamanagement/server/node_modules/hapi/lib/handler.js:96:36)',
  'at request._protect.run (/Users/robert.horvath/code/pos-datamanagement/server/node_modules/hapi/lib/handler.js:30:23)',
  'at internals.Protect.run (/Users/robert.horvath/code/pos-datamanagement/server/node_modules/hapi/lib/protect.js:64:5)',
  'at exports.execute (/Users/robert.horvath/code/pos-datamanagement/server/node_modules/hapi/lib/handler.js:24:22)',
  'at each (/Users/robert.horvath/code/pos-datamanagement/server/node_modules/hapi/lib/request.js:383:16)',
  'at iterate (/Users/robert.horvath/code/pos-datamanagement/server/node_modules/items/lib/index.js:36:13)',
  'at done (/Users/robert.horvath/code/pos-datamanagement/server/node_modules/items/lib/index.js:28:25)',
  'at internals.Auth._authenticate (/Users/robert.horvath/code/pos-datamanagement/server/node_modules/hapi/lib/auth.js:210:16)',
  'at internals.Auth.authenticate (/Users/robert.horvath/code/pos-datamanagement/server/node_modules/hapi/lib/auth.js:202:17)',
  'at each (/Users/robert.horvath/code/pos-datamanagement/server/node_modules/hapi/lib/request.js:383:16)',
  'at iterate (/Users/robert.horvath/code/pos-datamanagement/server/node_modules/items/lib/index.js:36:13)',
  'at done (/Users/robert.horvath/code/pos-datamanagement/server/node_modules/items/lib/index.js:28:25)',
  'at internals.state (/Users/robert.horvath/code/pos-datamanagement/server/node_modules/hapi/lib/route.js:357:16)',
  'at each (/Users/robert.horvath/code/pos-datamanagement/server/node_modules/hapi/lib/request.js:383:16)',
  'at iterate (/Users/robert.horvath/code/pos-datamanagement/server/node_modules/items/lib/index.js:36:13)',
  'at Object.exports.serial (/Users/robert.horvath/code/pos-datamanagement/server/node_modules/items/lib/index.js:39:9)',
  'at internals.Request._lifecycle (/Users/robert.horvath/code/pos-datamanagement/server/node_modules/hapi/lib/request.js:386:11)',
  'at internals.Request._execute (/Users/robert.horvath/code/pos-datamanagement/server/node_modules/hapi/lib/request.js:301:21)',
  'at Domain.request._protect.enter (/Users/robert.horvath/code/pos-datamanagement/server/node_modules/hapi/lib/connection.js:261:25)',
  'at Domain.run (domain.js:221:14)',
  'at internals.Protect.enter (/Users/robert.horvath/code/pos-datamanagement/server/node_modules/hapi/lib/protect.js:80:17)',
  'at Server.<anonymous> (/Users/robert.horvath/code/pos-datamanagement/server/node_modules/hapi/lib/connection.js:259:30)',
  'at emitTwo (events.js:106:13)',
  'at Server.emit (events.js:191:7)',
  'at HTTPParser.parserOnIncoming [as onIncoming] (_http_server.js:543:12)',
  'at HTTPParser.parserOnHeadersComplete (_http_common.js:105:23)' ]
Bilal-S commented 8 years ago

What does your VM template look like?

robhorvath commented 8 years ago

I'm using the default one that's part of serverless-offline.

#define( $loop )
{
#foreach($key in $map.keySet())
  "$util.escapeJavaScript($key)":
    "$util.escapeJavaScript($map.get($key))"
    #if( $foreach.hasNext ) , #end
#end
}
#end
{
  "body": $input.json("$"),
  "method": "$context.httpMethod",
  "principalId": "$context.authorizer.principalId",
  #set( $map = $input.params().header )
  "headers": $loop,
  #set( $map = $input.params().querystring )
  "query": $loop,
  #set( $map = $input.params().path )
  "path": $loop,
  #set( $map = $context.identity )
  "identity": $loop,
  #set( $map = $stageVariables )
  "stageVariables": $loop
}
Bilal-S commented 8 years ago

Would it be possible for you to provide a test project.zip (simplified with one function) that produces the error? Only your code files of course.

robhorvath commented 8 years ago

I've narrowed down the issue. I created an empty hello world service, which works fine. So I then looked at my project, and figured out that if I use a tool like Insomnia to request my endpoint, I get the error. If I request it via Chrome, it works.

I think it may come down to Content-Type in the requests being different between those environments. Insomnia is requesting application/json.

robhorvath commented 8 years ago

Further update - it's a tooling issue. Postman requests work fine. I'll need to troubleshoot the issue on my end for Insomnia. Thanks for your help!