我可以肯定我做过一些愚蠢的事情,但是我已经盯着这个呆了好几个小时了,我需要一个比我聪明的人。以下是aws-lambda函数中的js代码,该函数向mailchimp批处理API发出POST请求。目标是为列表的多个成员创建一批DELETE操作。它首先向dynamoDb发送一个请求,以获取正确的列表ID和API密钥,并且该请求没有任何障碍。当它对MailChimp进行API调用时,API响应200,但存在0个total_operations。当我向批处理发出GET请求以检查状态时,它告诉我该操作完成了0个操作,没有错误。我想念什么?
import { success, failure } from "./libs/response-lib";
import * as dynamoDbLib from "./libs/dynamodb-lib";
import Mailchimp from "mailchimp-api-v3";
export async function main(event, context, callback) {
const parsedUserId = event.requestContext.identity.cognitoIdentityId.split(":")[1];
const data = JSON.parse(event.body);
const params = {
TableName: "tr-users",
Key: { userId: parsedUserId },
ProjectionExpression: "mc_apiKey, listId"
};
try {
//get the user's contact list ID and API key
await dynamoDbLib.call("get", params)
.then((result) => {
//build operations array
var reqData = {};
reqData.operations = data.contacts.map(contact => {
var outputObject = {
method: "DELETE",
path: `lists/${result.Item.listId}/members/${contact}`
};
return outputObject;
});
//make batch post to mailchimp
var mailchimp = new Mailchimp(result.Item.mc_apiKey);
mailchimp.request({
method: "post",
path: "/batches",
data: reqData
}).then((response) => {
callback(null, success(response));
});
})
} catch (e) {
console.log(e);
callback(null, failure({ status: false }));
}
}
以下是该批处理的GET请求的响应:
{"id":"0c25e7f089","status":"finished","total_operations":0,"finished_operations":0,"errored_operations":0,"submitted_at":"2018-08-14T01:20:28+00:00","completed_at":"2018-08-14T01:20:34+00:00","response_body_url":"","_links":[{"rel":"parent","href":"https://us18.api.mailchimp.com/3.0/batches","method":"GET","targetSchema":"https://us18.api.mailchimp.com/schema/3.0/Definitions/Batches/CollectionResponse.json","schema":"https://us18.api.mailchimp.com/schema/3.0/CollectionLinks/Batches.json"},{"rel":"self","href":"https://us18.api.mailchimp.com/3.0/batches/0c25e7f089","method":"GET","targetSchema":"https://us18.api.mailchimp.com/schema/3.0/Definitions/Batches/Response.json"},{"rel":"delete","href":"https://us18.api.mailchimp.com/3.0/batches/0c25e7f089","method":"DELETE"}]}
任何见解都值得赞赏!
答案 0 :(得分:0)
以防像我这样愚蠢的人偶然发现这一点。我没有阅读api包装程序(mailchimp-api-v3)上的说明。批次具有不同的功能。工作代码:
import { success, failure } from "./libs/response-lib";
import * as dynamoDbLib from "./libs/dynamodb-lib";
import Mailchimp from "mailchimp-api-v3";
export async function main(event, context, callback) {
const parsedUserId = event.requestContext.identity.cognitoIdentityId.split(":")[1];
const data = JSON.parse(event.body);
const params = {
TableName: "tr-users",
Key: { userId: parsedUserId },
ProjectionExpression: "mc_apiKey, listId"
};
try {
//get the user's contact list ID and API key
await dynamoDbLib.call("get", params)
.then((result) => {
//build operations array
var reqData = {};
reqData["operations"] = data.contacts.map(contact => {
var outputObject = {
"method": "DELETE",
"path": `/lists/${result.Item.listId}/members/${contact}`
};
return outputObject;
});
console.log(JSON.stringify(reqData));
//make batch post to mailchimp
var mailchimp = new Mailchimp(result.Item.mc_apiKey);
mailchimp.batch(reqData.operations, {wait: false})
.then((result) => {
callback(null, success(result));
})
})
} catch (e) {
console.log(e);
callback(null, failure({ status: false }));
}
}