检查api标头中是否存在api令牌

我试图测试我的API调用中是否存在API令牌。传递给的标头是-

"host": [
          {
            "key": "Host",
            "value": "mydomain"
          }
        ],
        "x-api-key": [
          {
            "key": "x-api-key",
            "value": "mykey"
          }
        ]
      }

现在我的代码看起来像

 // Check if apikey is a part of the headers 
    if ( !headers.hasOwnProperty('x-api-key') || headers['x-api-key'][0].value!="mykey") { 
        const body = 'Unauthorized';
        const response = {
            status: 401,
            statusDescription: 'Unauthorized',
            body: body,
        };
        callback(null, response);
    }

My If case errors out instead of sending 401 if the headers are missing x-api-key altogether

  "errorMessage": "Cannot read property '0' of undefined",

我应该如何更改条件,以便在缺少标题键/值的情况下检查键/值对并且没有未定义的错误