我很确定我做了什么愚蠢的事,但是我已经盯着这个看了好几个小时了,我需要一个比我更聪明的人下面是aws-lambda函数中的js代码,该函数向mailchimp batches API发出POST请求。目标是为列表的多个成员创建一批删除操作。它首先向dynamoDb发出请求,以获取正确的列表ID和API键,这一切都进行得很顺利。当它对MailChimp进行API调用时,API响应200,但是没有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"}]}任何洞察力都是非常感谢的!
发布于 2018-08-17 08:16:23
以防像我这样的笨蛋偶然发现这一点。我没有阅读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 }));
}
}https://stackoverflow.com/questions/51832648
复制相似问题