如何使用异步函数在 AWS lambda 中调用 pinpoint.getUserEndpoints
How to use async function to call pinpoint.getUserEndpoints in AWS lambda
我正在使用 pinpoint api 来获取用户端点。我的代码看起来像这样。
const AWS = require('aws-sdk');
const pinpoint = new AWS.Pinpoint()
exports.handler = async (event, context) => {
console.log(JSON.stringify(event, null, 2));
event.Records.forEach(record => {
console.log(record.eventID);
console.log(record.eventName);
const params = {
ApplicationId: "secret", /* required */
UserId: "userId"/* required */
};
try {
var result = pinpoint.getUserEndpoints(params).promise();
console.log('data',result);
}
catch (err) {
console.log(err);
return
}
})
context.done(null, 'Successfully processed DynamoDB record'); // SUCCESS with message
};
我查看了云监视日志,上面写着 data Promise { <pending> }
我也在 forEach 里面试过这个。
var result = pinpoint.getUserEndpoints(params).promise();
result.then(function(data) {
console.log('Success',data);
}).catch(function(err) {
console.log('err',err);
});
但是我在 Cloud Watch 中找不到这部分的任何日志。
如何使用 pinpoint.getUserEndpoints(params).promise();
正确调用此异步函数?
你有点用你的初始 post 围绕着答案跳舞。尝试以下操作:
const AWS = require('aws-sdk');
const pinpoint = new AWS.Pinpoint()
exports.handler = async (event, context) => {
console.log(JSON.stringify(event, null, 2));
await Promise.all(event.Records.map(async (record) => {
console.log(record.eventID);
console.log(record.eventName);
const params = {
ApplicationId: "secret", /* required */
UserId: "userId"/* required */
};
const result = await pinpoint.getUserEndpoints(params).promise().catch(console.log);
console.log('data', result);
}));
return 'Successfully processed DynamoDB record'; // SUCCESS with message
};
我正在使用 pinpoint api 来获取用户端点。我的代码看起来像这样。
const AWS = require('aws-sdk');
const pinpoint = new AWS.Pinpoint()
exports.handler = async (event, context) => {
console.log(JSON.stringify(event, null, 2));
event.Records.forEach(record => {
console.log(record.eventID);
console.log(record.eventName);
const params = {
ApplicationId: "secret", /* required */
UserId: "userId"/* required */
};
try {
var result = pinpoint.getUserEndpoints(params).promise();
console.log('data',result);
}
catch (err) {
console.log(err);
return
}
})
context.done(null, 'Successfully processed DynamoDB record'); // SUCCESS with message
};
我查看了云监视日志,上面写着 data Promise { <pending> }
我也在 forEach 里面试过这个。
var result = pinpoint.getUserEndpoints(params).promise();
result.then(function(data) {
console.log('Success',data);
}).catch(function(err) {
console.log('err',err);
});
但是我在 Cloud Watch 中找不到这部分的任何日志。
如何使用 pinpoint.getUserEndpoints(params).promise();
正确调用此异步函数?
你有点用你的初始 post 围绕着答案跳舞。尝试以下操作:
const AWS = require('aws-sdk');
const pinpoint = new AWS.Pinpoint()
exports.handler = async (event, context) => {
console.log(JSON.stringify(event, null, 2));
await Promise.all(event.Records.map(async (record) => {
console.log(record.eventID);
console.log(record.eventName);
const params = {
ApplicationId: "secret", /* required */
UserId: "userId"/* required */
};
const result = await pinpoint.getUserEndpoints(params).promise().catch(console.log);
console.log('data', result);
}));
return 'Successfully processed DynamoDB record'; // SUCCESS with message
};