[英]Handling a event return on AWS lambda
我目前正在使用 lambda 發送 API 詳細信息以獲取訪問令牌。 然后我想保存該訪問令牌以供以后使用。 似乎它沒有保存訪問密鑰。 有小費嗎? Lambda:
var axios = require('axios');
var qs = require('qs');
exports.handler = async (event) => {
var data = qs.stringify({
'client_id': 'theID',
'client_secret': 'theSecret',
'scope': 'ski',
'grant_type': 'client_credentials'
});
var config = {
method: 'post',
url: 'myendpoint',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
data : data,
};
const postData = await axios.post('myendpoint',data, config)
console.log(postData);
const response = {
statusCode: 200,
body: JSON.stringify(postData.response.data.access_token),
};
var mykey = data.access_token;
return response;
};
這是我的錯誤報告:
Response
{
"errorType": "TypeError",
"errorMessage": "Cannot read property 'data' of undefined",
"trace": [
"TypeError: Cannot read property 'data' of undefined",
" at Runtime.exports.handler (/var/task/index.js:26:48)",
" at processTicksAndRejections (internal/process/task_queues.js:93:5)"
]
}
這是我的日志:
Function Logs
gOutput],
agent: Agent {
_events: [Object: null prototype],
_eventsCount: 2,
_maxListeners: undefined,
defaultPort: 443,
protocol: 'https:',
options: [Object],
requests: {},
sockets: [Object],
freeSockets: {},
keepAliveMsecs: 1000,
keepAlive: false,
maxSockets: Infinity,
maxFreeSockets: 256,
scheduling: 'fifo',
maxTotalSockets: Infinity,
totalSocketCount: 1,
maxCachedSessions: 100,
_sessionCache: [Object],
[Symbol(kCapture)]: false
},
socketPath: undefined,
method: 'POST',
maxHeaderSize: undefined,
insecureHTTPParser: undefined,
path: 'API/connect/token',
_ended: true,
res: IncomingMessage {
_readableState: [ReadableState],
_events: [Object: null prototype],
_eventsCount: 3,
_maxListeners: undefined,
socket: [TLSSocket],
httpVersionMajor: 1,
httpVersionMinor: 1,
httpVersion: '1.1',
complete: true,
headers: [Object],
rawHeaders: [Array],
trailers: {},
rawTrailers: [],
aborted: false,
upgrade: false,
url: '',
method: null,
statusCode: 200,
statusMessage: 'OK',
client: [TLSSocket],
_consuming: false,
_dumped: false,
req: [Circular *1],
responseUrl: 'myendpoint',
redirects: [],
[Symbol(kCapture)]: false,
[Symbol(RequestTimeout)]: undefined
},
aborted: false,
timeoutCb: null,
upgradeOrConnect: false,
parser: null,
maxHeadersCount: null,
reusedSocket: false,
host: 'myhost',
protocol: 'https:',
_redirectable: Writable {
_writableState: [WritableState],
_events: [Object: null prototype],
_eventsCount: 2,
_maxListeners: undefined,
_options: [Object],
_ended: true,
_ending: true,
_redirectCount: 0,
_redirects: [],
_requestBodyLength: 81,
_requestBodyBuffers: [],
_onNativeResponse: [Function (anonymous)],
_currentRequest: [Circular *1],
_currentUrl: 'myendpoint',
[Symbol(kCapture)]: false
},
[Symbol(kCapture)]: false,
[Symbol(kNeedDrain)]: false,
[Symbol(corked)]: 0,
[Symbol(kOutHeaders)]: [Object: null prototype] {
accept: [Array],
'content-type': [Array],
'user-agent': [Array],
'content-length': [Array],
host: [Array]
}
},
data: {
access_token: 'ThisIsMyAccessKeyIGetBackAndWantToSave',
expires_in: 3600,
token_type: 'Bearer'
}
}
2021-04-10T10:41:22.858Z e5e3bc3a-f08d-4077-8897-709225abd5f8 ERROR Invoke Error {"errorType":"TypeError","errorMessage":"Cannot read property 'data' of undefined","stack":["TypeError: Cannot read property 'data' of undefined"," at Runtime.exports.handler (/var/task/index.js:26:48)"," at processTicksAndRejections (internal/process/task_queues.js:93:5)"]}
END RequestId: e5e3bc3a-f08d-4077-8897-709225abd5f8
REPORT RequestId: e5e3bc3a-f08d-4077-8897-709225abd5f8 Duration: 1631.22 ms Billed Duration: 1632 ms Memory Size: 128 MB Max Memory Used: 19 MB
Request ID
e5e3bc3a-f08d-4077-8897-709225abd5f8
我正在做的是將客戶端詳細信息發送到此端點,它會返回一個在一段時間內有效的訪問密鑰。 它確實給了一個不需要的龐大身體,我只需要將信息保存在數據中:但即使我得到正確的響應,它也會不斷給我提到的錯誤。
謝謝似乎我所要做的就是正確定義我的密鑰
var mykey = postData.data.accesskey
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.