首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >调用Node 8.10 Lambda时JSON无效

调用Node 8.10 Lambda时JSON无效
EN

Stack Overflow用户
提问于 2018-10-08 12:45:48
回答 1查看 560关注 0票数 0

在成功登录后,我使用lambda和cognito来写入dynamoDB。

Node8.10具有与promise和asycn/await不同的布局。callback(null, event)返回对我的不起作用。现在有没有人知道如何用Node8.10解决Invalid lambda function output : Invalid JSON问题。

代码语言:javascript
复制
// 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);
};   

谢谢

EN

回答 1

Stack Overflow用户

发布于 2018-10-08 16:09:32

对于建议的async/await节点8方法,您应该使用以下方法来构建函数:

代码语言:javascript
复制
async function handler(event) {
  const response = doSomethingAndReturnAJavascriptObject();
  return response;
}

之所以会出现这个错误,是因为无论您返回什么,都不是可以解析为JSON对象的东西。

如果看不到代码,就很难进一步调试。我预计您可能会意外地没有使用.promise()版本的发电机/认知API调用,这会导致您返回一个Promise而不是await

注:如果你觉得Node8更简单,你仍然可以在Node8中使用“旧的”callback()方法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52695513

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档