我正在嘗試在打字稿環境中定義 lambda 處理程式。
const sampleFunc = async (event) => {
console.log('request:', JSON.stringify(event, undefined, 2));
return {
statusCode: 200,
headers: { 'Content-Type': 'text/plain' },
body: `Hello, CDK! You've hit ${event.path}\n`,
};
};
exports.handler = sampleFunc(event);
如果它是洗掉線(無法在問題中格式化)并且編譯器說它已被棄用。
棄用訊息:
'event' is deprecated.ts(6385)
lib.dom.d.ts(17314, 5): The declaration was marked as deprecated here.
但是,對于相同的代碼,當我沒有單獨定義函式時,它可以作業。
exports.handler = async function (event) {
console.log('request:', JSON.stringify(event, undefined, 2));
return {
statusCode: 200,
headers: { 'Content-Type': 'text/plain' },
body: `Hello, CDK! You've hit ${event.path}\n`,
};
};
uj5u.com熱心網友回復:
我認為錯誤不在于函式定義本身,而在于您如何匯出它。
您必須匯出函式而不是呼叫它:
錯誤的:
exports.handler = sampleFunc(event);
正確的:
exports.handler = sampleFunc;
您也可以直接匯出函式:
exports.handler = async (event) => {
console.log('request:', JSON.stringify(event, undefined, 2));
return {
statusCode: 200,
headers: { 'Content-Type': 'text/plain' },
body: `Hello, CDK! You've hit ${event.path}\n`,
};
};
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/450564.html
