【发布时间】:2020-06-29 20:18:00
【问题描述】:
我正在尝试创建一个 lambda API 调用,它将遍历 DynamoDB 表的结果并对 3rd 方系统执行不同的 API 调用。
扫描功能正常运行,因为它返回了正确的数据。
我在这里遇到了迭代问题,因为它不会触发对 Linkedin 的 Axios 调用,但是,该函数被称为 console.log bid 它会显示在云手表中。
云手表没有显示任何错误。
理想情况下,当完成并添加一个 API 调用 + 修改 dynamo DB 后,下面的功能将类似于一个 cron 作业,它会在一小时内每 x 次触发一次。
如果有人能建议我如何处理这个问题/提出不同的方法,我将不胜感激。
const dynamodb = require("aws-sdk/clients/dynamodb");
const axios = require("axios");
const docClient = new dynamodb.DocumentClient();
const tableName = "**************";
exports.executeJobs = async(event) => {
const { httpMethod, path } = event;
if (httpMethod !== "GET") {
throw new Error(
`Method only accepts GET method, you tried: ${httpMethod} method.`
);
}
console.log("received:", JSON.stringify(event));
var params = {
TableName: tableName,
ProjectionExpression: "#timestamper, #userId,#campaignId, #type,#info,#token",
FilterExpression: "#timestamper < :timestamper",
ExpressionAttributeNames: {
"#timestamper": "timestamper",
"#type": "type",
"#info": "info",
"#userId": "userId",
"#campaignId": "campaignId",
"#token": "token",
},
ExpressionAttributeValues: {
":timestamper": Date.now(),
},
};
console.log("Scanning Jobs table.");
let scanResults = [];
let items;
do {
items = await docClient.scan(params).promise();
items.Items.forEach(async function (item) {
scanResults.push(item)
try {
const response = await bidIt(item.campaignId, item.info.currency, item.token, item.info.bid);
console.log(response);
} catch (error) {
console.error(error);
}
});
params.ExclusiveStartKey = items.LastEvaluatedKey;
} while (typeof items.LastEvaluatedKey != "undefined");
const response = {
statusCode: 200,
body: JSON.stringify(scanResults),
};
console.log(
`response from: ${path} statusCode: ${response.statusCode} body: ${response.body}`
);
return response;
};
async function bidIt(campaignId, currency, token, bid) {
console.log("bidIT");
try {
axios
.post(
"https://api.linkedin.com/v2/adCampaignsV2/" + campaignId, {
patch: {
$set: {
unitCost: {
amount: bid,
currencyCode: currency,
},
},
},
}, {
headers: {
"Content-Type": "application/json",
"X-RestLi-Method": "PARTIAL_UPDATE",
Authorization: "Bearer " + token,
},
}
)
.then((result) => {
return UpdateCpc(campaignId, currency, token, bid);
});
} catch (error) {
console.log("error", error);
// appropriately handle the error
}
}
更新: 谢谢,Kalev 的回复,我已经修改了代码,但是仍然没有等待 API 调用并关闭查询。 截图来自云手表。 (我将 bidIt 改为 getMinBid 名称)
async function getMinBid(campaignId, currency, token, bid) {
try {
console.log("getMinBid");
let result = await axios.post(
"https://api.linkedin.com/v2/adCampaignsV2/" + campaignId, {
patch: {
$set: {
unitCost: {
amount: bid,
currencyCode: currency,
},
},
},
}, {
headers: {
"Content-Type": "application/json",
"X-RestLi-Method": "PARTIAL_UPDATE",
Authorization: "Bearer " + token,
},
}
)
.then((result) => {
console.log("axios success");
console.log(JSON.stringify(result));
let minCost = result.data.split("lower than ").pop();
minCost = parseFloat(minCost) + bid;
});
return UpdateCpc(campaignId, currency, token, minCost);
} catch (error) {
console.log('test bid')
console.log("error", error);
// appropriately handle the error
}
}
【问题讨论】:
标签: node.js amazon-web-services aws-lambda axios amazon-dynamodb