当我调用端点时,出现以下错误:
Streaming new logs for function: "placeBid"
INIT_START Runtime - Version: nodejs:20.v42 Runtime Version ARN: arn:aws:lambda:sa-east-1::runtime:af2737389420ddcdbcdc4db3585842c334ad2482127c15295905f54a62feefc6
2024-10-20 17:41:33.920 - undefined -
ERROR Uncaught Exception {"errorType":"Error","errorMessage":"Cannot find module '/var/task/lib/commonMiddleware' imported from /var/task/handlers/getAuction.js","code":"ERR_MODULE_NOT_FOUND","url":"file:///var/task/lib/commonMiddleware","stack":\["Error \[ERR_MODULE_NOT_FOUND\]: Cannot find module '/var/task/lib/commonMiddleware' imported from /var/task/handlers/getAuction.js"," at finalizeResolution (node:internal/modules/esm/resolve:269:11)"," at moduleResolve (node:internal/modules/esm/resolve:937:10)"," at moduleResolveWithNodePath (node:internal/modules/esm/resolve:1173:14)"," at defaultResolve (node:internal/modules/esm/resolve:1216:79)"," at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:540:12)"," at ModuleLoader.resolve (node:internal/modules/esm/loader:509:25)"," at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:239:38)"," at ModuleWrap.\<anonymous\> (node:internal/modules/esm/module_job:96:40)"," at link (node:internal/modules/esm/module_job:95:36)"\]}
INIT_REPORT Init Duration: 194.75 ms Phase: init Status: error Error Type: Runtime.Unknown
2024-10-20 17:41:34.689 - undefined -
ERROR Uncaught Exception {"errorType":"Error","errorMessage":"Cannot find module '/var/task/lib/commonMiddleware' imported from /var/task/handlers/getAuction.js","code":"ERR_MODULE_NOT_FOUND","url":"file:///var/task/lib/commonMiddleware","stack":\["Error \[ERR_MODULE_NOT_FOUND\]: Cannot find module '/var/task/lib/commonMiddleware' imported from /var/task/handlers/getAuction.js"," at finalizeResolution (node:internal/modules/esm/resolve:269:11)"," at moduleResolve (node:internal/modules/esm/resolve:937:10)"," at moduleResolveWithNodePath (node:internal/modules/esm/resolve:1173:14)"," at defaultResolve (node:internal/modules/esm/resolve:1216:79)"," at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:540:12)"," at ModuleLoader.resolve (node:internal/modules/esm/loader:509:25)"," at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:239:38)"," at ModuleWrap.\<anonymous\> (node:internal/modules/esm/module_job:96:40)"," at link (node:internal/modules/esm/module_job:95:36)"\]}
INIT_REPORT Init Duration: 949.46 ms Phase: invoke Status: error Error Type: Runtime.Unknown
START - b268505b-edc7-49c1-a4cb-27cc869d550f - Version: $LATEST
END - b268505b-edc7-49c1-a4cb-27cc869d550f
REPORT - b268505b-edc7-49c1-a4cb-27cc869d550f
Duration: 960.51 ms Billed Duration: 961 ms Memory Size: 256 MB Max Memory Used: 73 MB Status: error Error Type: Runtime.Unknown
这是我的端点运行它的功能:
import AWS from 'aws-sdk';
import createError from 'http-errors';
import middy from '@middy/core';
import httpJsonBodyParser from '@middy/http-json-body-parser';
import httpEventNormalizer from '@middy/http-event-normalizer';
import httpErrorHandler from '@middy/http-error-handler';
import { getAuctionById } from './getAuction';
const dynamoDB = new AWS.DynamoDB.DocumentClient();
async function placeBid(event, context) {
const { id } = event.pathParameters;
const { amount } = event.body;
const auction = await getAuctionById(id);
console.log('AUCTIONS', auction);
if (amount <= auction.highestBid.amount) {
throw new createError.Forbidden(
`Your bid must be higher than ${auction.highestBid.amount}!`,
);
}
const params = {
TableName: process.env.AUCTIONS_TABLE_NAME,
Key: { id },
UpdateExpression: 'set highestBid.amount = :amount',
ExpressionAttributeValues: {
':amount': amount,
},
ReturnValues: 'ALL_NEW',
};
let updatedAuction;
try {
const result = await dynamoDB.update(params).promise();
updatedAuction = result.Attributes;
} catch (error) {
console.error(error);
throw new createError.InternalServerError(error);
}
return {
statusCode: 200,
body: JSON.stringify(updatedAuction),
};
}
export const handler = middy(placeBid)
.use(httpJsonBodyParser())
.use(httpEventNormalizer())
.use(httpErrorHandler());
我尝试更改导入 getAuctionById 函数的方式以及导出此函数的方式,下面是我导出此函数的代码:
import AWS from 'aws-sdk';
import createError from 'http-errors';
import commonMiddleware from '../lib/commonMiddleware';
const dynamoDB = new AWS.DynamoDB.DocumentClient();
const getAuctionById = async id => {
let auction;
try {
const result = await dynamoDB
.get({
TableName: process.env.AUCTIONS_TABLE_NAME,
Key: { id },
})
.promise();
auction = result.Item;
} catch (error) {
console.error(error);
throw new createError.InternalServerError(error);
}
return auction;
};
async function getAuction(event, context) {
const { id } = event.pathParameters;
const auction = await getAuctionById(id);
if (!auction) {
throw new createError.NotFound(`Auction with ID "${id}" not found!`);
}
return {
statusCode: 200,
body: JSON.stringify(auction),
};
}
export const handler = commonMiddleware(getAuction);
export { getAuctionById };
我仍然尝试直接在函数声明中进行导出,如下所示:
module.exports = getAuctionById
但是我没有成功!
未捕获的异常错误是由一组额外的 '} 或 {' 引起的。 查找不必要的括号并将其从代码中删除。 我为你指出并评论了它。在您的出口代码内。确保再次检查所有代码,尤其是代码中括号周围的代码。
import AWS from 'aws-sdk';
import createError from 'http-errors';
import commonMiddleware from '../lib/commonMiddleware';
const dynamoDB = new AWS.DynamoDB.DocumentClient();
const getAuctionById = async id => {
let auction;
try {
const result = await dynamoDB
.get({
TableName: process.env.AUCTIONS_TABLE_NAME,
Key: { id },
})
.promise();
auction = result.Item;
} catch (error) {
console.error(error);
throw new createError.InternalServerError(error);
//} <--- Remove this here:
return auction;
};
async function getAuction(event, context) {
const { id } = event.pathParameters;
const auction = await getAuctionById(id);
if (!auction) {
throw new createError.NotFound(`Auction with ID "${id}" not found!`);
}
return {
statusCode: 200,
body: JSON.stringify(auction),
};
//} <--- Remove this
export const handler = commonMiddleware(getAuction);
export { getAuctionById };