在成功登录后,我使用lambda和cognito来写入dynamoDB。
Node8.10具有与promise和asycn/await不同的布局。callback(null, event)返回对我的不起作用。现在有没有人知道如何用Node8.10解决Invalid lambda function output : Invalid JSON问题。
// Load the AWS SDK for Node.js
var AWS = require('aws-sdk');
// Set the region
//AWS.config.update({region: 'REGION'});
// Create DynamoDB document client
var docClient = new AWS.DynamoDB.DocumentClient({apiVersion: '2012-08-10'});
exports.myHandler = async (event, context, callback) => {
// TODO implement
console.log ("Authentication successful");
console.log ("Trigger function =", event.triggerSource);
console.log ("User pool = ", event.userPoolId);
console.log ("App client ID = ", event.callerContext.clientId);
console.log ("User ID = ", event.userName);
const params = {
TableName: 'xxxx',
Item: {
'userId': event.userName,
'systemUpdateDate': new Date().toJSON()
}
};
let putItem = new Promise((res, rej) => {
docClient.put(params, function(err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("Success", data);
}
});
});
const result = await putItem;
console.log(result);
// Return to Amazon Cognito
callback(null, event);
}; 谢谢
发布于 2018-10-08 16:09:32
对于建议的async/await节点8方法,您应该使用以下方法来构建函数:
async function handler(event) {
const response = doSomethingAndReturnAJavascriptObject();
return response;
}之所以会出现这个错误,是因为无论您返回什么,都不是可以解析为JSON对象的东西。
如果看不到代码,就很难进一步调试。我预计您可能会意外地没有使用.promise()版本的发电机/认知API调用,这会导致您返回一个Promise而不是await。
注:如果你觉得Node8更简单,你仍然可以在Node8中使用“旧的”callback()方法。
https://stackoverflow.com/questions/52695513
复制相似问题