Whenever I test my lambda with an api gateway sample event body the only thing that returns in the callback outside of the switch statement which leads me to believe that the switch case is never triggering. I have been debugging for a few hours now but cant seem to figure out why. please help
Lambda.js
exports.handler = (event, context, callback) => {
let switchParams = event.httpMethod && event.path
switch(switchParams){
case event.httpMethod === "GET" && event.path === "/development/api/brands":
let brandsRes = getBrands()
callback(null, brandsRes)
break;
case event.httpMethod === "GET" && event.path === "/development/api/sku" :
let skuRes = getitem()
callback(null, skuRes)
break;
case event.httpMethod === "GET" && event.path === "/development/api/brandsbysku":
callback(null, getitembybrand(event.queryStringParameters.brand))
break;
case event.httpMethod === "GET" && event.path === "/development/api/test":
let test = buildResponse(200, [{"message":"hello"}])
callback(null, test)
break;
}
callback(null, {})
}
function buildResponse(statusCode, body) {
return {
statusCode: statusCode,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin':'*',
'Access-Control-Allow-Methods':'GET,OPTIONS'
},
body: JSON.stringify(body),
isBase64Encoded: false
}
}
sample event body
{
"body": "eyJ0ZXN0IjoiYm9keSJ9",
"resource": "development/api/test",
"path": "development/api/test",
"httpMethod": "GET",
"isBase64Encoded": false,
"headers": {
"Accept": "text/html,application/xhtml xml,application/xml;q=0.9,image/webp,*/*;q=0.8,application/json",
"Accept-Encoding": "gzip, deflate, sdch",
"Accept-Language": "en-US,en;q=0.8",
"Cache-Control": "max-age=0",
"CloudFront-Forwarded-Proto": "https",
"CloudFront-Is-Desktop-Viewer": "true",
"CloudFront-Is-Mobile-Viewer": "false",
"CloudFront-Is-SmartTV-Viewer": "false",
"CloudFront-Is-Tablet-Viewer": "false",
"CloudFront-Viewer-Country": "US",
"Host": "1234567890.execute-api.us-east-1.amazonaws.com",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Custom User Agent String",
"Via": "1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)",
"X-Amz-Cf-Id": "cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA==",
"X-Forwarded-For": "127.0.0.1, 127.0.0.2",
"X-Forwarded-Port": "443",
"X-Forwarded-Proto": "https"
}
CodePudding user response:
Typically, switch statements are written like this:
switch (some value) {
case value1:
do something1
break
case value2:
do something2
break
}
What you have written looks like this:
switch (an expression that evaluates to event.path) {
case an expression that evaluates to true or false:
do something1
break
case an expression that evaluates to true or false:
do something2
break
}
The original code does not work because none of the case values match the switch value and hence no case is executed. All of the case values are booleans (true or false) but the switchParams evaluates to the event path, which is a string such as "/api/v1/users/" and not a boolean.
There is a legitimate switch (true)
pattern that goes as follows, but it's typically used when the programmer wants fall-through behavior and doesn't fit your needs:
switch (true) {
case an expression that evaluates to true or false:
do something1
case an expression that evaluates to true or false:
do something2
}
The simplest solution here is to use if/else, as follows:
if (event.httpMethod === "GET" && event.path === "/development/api/brands") {
callback(null, getBrands())
} else if (event.httpMethod === "GET" && event.path === "/development/api/sku") {
callback(null, getitem())
// etc. etc. for other cases
} else {
callback(null, {})
}
Or potentially:
if (event.httpMethod !== "GET") {
callback(null, {})
return
}
switch (event.path) {
case "/development/api/brands":
callback(null, getBrands())
break;
case "/development/api/sku":
callback(null, getitem())
break;
// etc. etc.
}