首页 > 解决方案 > 使用本地调用的 lambda 函数向外部 API 发出请求并将数据记录到控制台

问题描述

我想在 Lambda 函数的帮助下使用新发布的 API 访问 Notion 数据库。

应用程序.js

/**
 *
 * Event doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format
 * @param {Object} event - API Gateway Lambda Proxy Input Format
 *
 * Context doc: https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-context.html 
 * @param {Object} context
 *
 * Return doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html
 * @returns {Object} object - API Gateway Lambda Proxy Output Format
 * 
 */

require('dotenv').config()
const { Client } = require('@notionhq/client');

const notion = new Client({
    auth: process.env.NOTION_TOKEN,
})

const getBDays = async () => {
    const payload = {
        path: `databases/${process.env.NOTION_DB_ID}/query`,
        method: 'POST'
    }
    const { results } = await notion.request(payload)
    console.log("DATA", results)
}

exports.lambdaHandler = async (event, context) => {

    try {
        getBDays()
    } catch (err) {
        console.log(err);
        return err;
    }
};

当我使用 SAM CLI 在本地调用此函数时,我看不到预期的输出。这是我在控制台中看到的

Invoking app.lambdaHandler (nodejs14.x)
Skip pulling image and use local one: amazon/aws-sam-cli-emulation-image-nodejs14.x:rapid-1.23.0.

Mounting /Volumes/MacOS/Projects/Projects/4. Remembery/remembery/hello-world as /var/task:ro,delegated inside runtime container
START RequestId: 423f4ea6-aeae-4780-bb36-af32db051f2b Version: $LATEST
END RequestId: 423f4ea6-aeae-4780-bb36-af32db051f2b
REPORT RequestId: 423f4ea6-aeae-4780-bb36-af32db051f2b  Init Duration: 0.20 ms  Duration: 1179.88 ms    Billed Duration: 1200 ms      Memory Size: 128 MB     Max Memory Used: 128 MB

我无法弄清楚这里发生了什么。

标签: node.jsamazon-web-servicesaws-lambda

解决方案


推荐阅读