我注意到我的AWS Lambda函数有一些奇怪的行为。
这是Lambda的代码:
import { TwitterApi } from 'twitter-api-v2';
const client = new TwitterApi({
appKey: 'APP_KEY',
appSecret: 'APP_SECRET',
accessToken: 'ACCESS_TOKEN',
accessSecret: 'ACCESS_SECRET',
});
const rwClient = client.readWrite
exports.handler = async function (event: any) {
event.Records.forEach((record: any) => {
console.log('Event Name: %s', record.eventName);
console.log('DynamoDB Record: %j', record.dynamodb);
switch (record.eventName) {
case "INSERT":
rwClient.v1.tweet('Hello, this is a test.');
break;
default:
break;
}
});
};如果我将一个元素插入到DynamoDb中,就会触发一个EventHandler,然后调用rwClient.v1.tweet('Hello, this is a test.');
从理论上讲,这是可行的。如果在语句前后添加一个console.log(),则将执行两个日志。但是,当我查看我连接到的twitter帐户时,并没有发布推特。
如果我在https://npm.runkit.com上运行以下代码片段,tweet将显示在帐户中:
const twitter_api_v2_1 = require("twitter-api-v2");
const client = new twitter_api_v2_1.TwitterApi({
appKey: 'APP_KEY',
appSecret: 'APP_SECRET',
accessToken: 'ACCESS_TOKEN',
accessSecret: 'ACCESS_SECRET',
});
const rwc = client.readWrite;
rwc.v1.tweet('Hello, this is a test.');有人知道我如何使Lambda函数工作的解决方案吗?
发布于 2022-09-26 20:12:24
rwClient.v1.tweet()可能是一个async方法,您的请求在异步执行之前终止。
尝试等待任务(同时进行):
exports.handler = async function (event: any) {
const tasks = event.Records.map(async (record: any) => {
console.log('Event Name: %s', record.eventName);
console.log('DynamoDB Record: %j', record.dynamodb);
switch (record.eventName) {
case "INSERT":
await rwClient.v1.tweet('Hello, this is a test.');
break;
default:
break;
}
});
await Promise.all(tasks);
};或等待……(顺序):
exports.handler = async function (event: any) {
for (const record of event.Records) {
console.log('Event Name: %s', record.eventName);
console.log('DynamoDB Record: %j', record.dynamodb);
switch (record.eventName) {
case "INSERT":
await rwClient.v1.tweet('Hello, this is a test.');
break;
default:
break;
}
}
};https://stackoverflow.com/questions/73858827
复制相似问题