LoginSignup
2
2

More than 1 year has passed since last update.

AWSLambda(Nodejs)でプロキシ統合のAPIGatewayから渡ってきたevent.bodyがundefinedになる

Last updated at Posted at 2021-06-15

はじめに

AWSLambdaのNodejsでプロキシ統合のAPIGateway経由で
event.bodyを取得しようとした際にundefinedになりハマったので、メモ

AWSLambdaのプロキシ統合とは

該当のコード

APIGatewayから渡ってきたパラメータを取得しようと参照するとundefinedの参照エラーになる。

exports.handler = async (event, context) => {
    // event.bodyがundefinedになる
    JSON.parse(event.body).hoge
}

解決した方法

以下のように先にif文で確認してから参照するとundefinedにならない。

exports.handler = async (event, context) => {
    if (event.body) {
        JSON.parse(event.body).hoge
    }
}

終わりに

上記の方法で問題は解決できたが、
なぜundefinedになるのかがよくわからないが公式の実装サンプルなどでもif文で確認している。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2